swift - 将 textfield.text 与 firebase 字符串 swift 进行比较

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

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

之前对我之前问题的回答表明我需要更改“.”。到电子邮件地址中的“,”。所以 @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”的节点看起来像这样

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 中?

(我确实在比较的时候将'.'转换为',')

最佳答案

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

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

这是要使用的结构

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")
}

关于swift - 将 textfield.text 与 firebase 字符串 swift 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41386373/

相关文章:

swift - 共享数据源时如何识别哪个 UIPickerView 收到事件

arrays - FOR IN 循环出错

swift - 合并 SKShapeNode 和 SKLabelNode

node.js - 如何使用 typescript 初始化 Firebase 云功能中的管理员凭据?

javascript - 如何使用 Firebase 数据库(react-native)在 ​​ListView 末尾添加 "Load More"

ios - 自定义 nspredicate 在两个距离之间

swift - 如何做某事(例如 : print ("hi")) in NavigationLink before moving to a destinationView

ios - FireBase 错误信号 SIGABRT

javascript - Firebase:如何在所有初始 "child_added"调用之后运行回调?

swift - 使用 Where 语句从 Firebase 计算 Swift 中的记录