【问题标题】:compare textfield.text to firebase string swift将 textfield.text 与 firebase 字符串 swift 进行比较
【发布时间】:2017-05-14 04:08:30
【问题描述】:

我在 Firebase 中有一个数据库,其中包含单独的用户节点。在每个用户的节点中都将包含与他们相关的数据,并且是私有的。除此之外,我想创建一个节点,它只是注册电子邮件的集合。原因是当用户在登录 VC 上并且用户键入电子邮件时。如果电子邮件已经注册,图像视图将变为绿色。但是,如果电子邮件不在数据库中(或者如果它与电子邮件地址格式不匹配),则图像将为红色。

我先前问题的先前答案表明我需要更改“。”到电子邮件地址中的“,”。所以@gmail.com 将被存储为 gmail,com

我记下了。

FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in

    if error == nil {

      let email = firstContainerTextField.text ?? ""

      let newString = email.replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)

      self.ref.child("users").child("allUsers").child(newString).setValue(true)

      self.ref.child("users").child((user?.uid)!).setValue(["Email": email])

      FIRAuth.auth()!.signIn(withEmail: email,
                             password: password)

    } else {
      //registration failure
    }

这是来自新用户 VC 的代码(部分)。

所以说“用户”和“所有用户”的节点在 Firebase 控制台上看起来像这样

users
    allUsers
        bob@bob,com: true
        ted@ted,com: true

“真实”部分只是为了让我可以将 bob@bob,com 放到数据库中……真实部分永远不会用于任何事情。

在登录 VC 时,我真的不知道该怎么做

之前的回答说要使用

hasChildren() 

我使用了它,然后用谷歌搜索了如何处理它 我尝试使用类似的东西

ref.child("users").child("allUsers").queryEqual(toValue: newString)
  .observe(.value, with: { snapshot in

if snapshot.hasChildren() {

    for child in snapshot.children.allObjects as! [FIRDataSnapshot] {
    ....


}



  });

但我似乎无能为力。

如何简单地查看 textfield.text == 电子邮件是否已存储在 firebase 中?

(比较时我确实将“.”转换为“,”)

【问题讨论】:

    标签: swift firebase firebase-realtime-database firebase-authentication


    【解决方案1】:

    请不要使用电子邮件地址作为密钥。电子邮件地址是动态的,并且可能会更改(就像用户想要更改它一样),如果他们这样做了,您手上就会一团糟,因为直接使用该电子邮件的每个节点都将被删除并重新创建。

    最佳做法是将密钥与其包含的数据分离。

    这是要使用的结构

    emails
      -Yiaisjpa90is
        email: "dude@test.com"
      -Yijs9a9js09a
        email: "thing@test.com"
    

    然后您只需在电子邮件节点中查询您要查找的电子邮件,如果存在则进行相应处理。

    还有一些代码

    emailsRef.queryOrdered(byChild: "email").queryEqual(toValue: "dude@test.com")
             .observe(.value, with: { snapshot in
         if snapshot.value is NSNull {
            print("the snapshot was null, no email found")
         } else {
            print("email was found, YIPEE")
         }  
    })
    

    为了完整起见,使用起来会更快捷

    if snapshot.exists() {
      print("found it")
    } else {
      print("no email found")
    }
    

    【讨论】:

    • 我有你写的代码,并且有一次它实际上正在做某事,但是即使我在数据库中有它,它也只会打印“未找到电子邮件”
    • @RubberDucky4444 好吧,如果您的 Firebase 结构与我发布的相同,并且代码相同,那么它必须像我从项目中复制和粘贴一样工作。确保你的 emailsRef = rootRef.child("emails") 也是如此。
    • 我唯一改变的是 'toValue: "dude@test.com")' 到 'toValue: email)' 因为电子邮件是我想要比较的对象
    • 我遇到了小写与大写的问题,谢谢!
    • Firebase 是否比较表单文本字段中输入的内容与后端 json 对中存储的内容之间是否区分大小写?!
    猜你喜欢
    • 2012-10-06
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多