compare textfield.text to firebase string swift



我在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 Console上说"用户"one_answers" Alluser"的节点看起来像这样

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

" true"部分就是这样,我可以将Bob@Bob,com上com到数据库...真正的部分永远不会用于任何事物。

在VC中的日志上,我不知道该怎么做

先前的答案说使用

hasChildren() 

我使用了它,然后用Google搜索了该如何处理我尝试使用这样的东西

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中的电子邮件?

(比较时,我确实将'。''转换为')

请不要将电子邮件地址用作密钥。电子邮件地址是动态的,可能会更改(如用户想要更改),如果这样做,您的手上会乱七八糟,因为直接使用该电子邮件的每个节点都将被删除和重新创建。<<<<<</p>

最佳实践是将密钥与它们所包含的数据分解。

这是使用

的结构
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")
}

最新更新