将 textfield.text 与 firebase 字符串 swift 进行比较 [英] compare textfield.text to firebase string swift

查看:16
本文介绍了将 textfield.text 与 firebase 字符串 swift 进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a database in Firebase that will have individual user nodes. In each user's node will be data pertaining to them and will be private. In addition to that I want to create a node that is JUST a collection of registered emails. The reason is when a user is on the Sign In VC and the user types an email in..If the email is already registered an image view will turn green. However, if the email is not in the database (or if it doesn't match email address format) the image will be red.

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

A previous answer on my previous question(s) illustrated that I need to change the '.' to a ',' in email addresses. So @gmail.com would be stored as 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
    }

这是来自 New User VC 的代码(部分).

This is the code from the New User VC (partial).

所以说users"和allUsers"的节点在 Firebase 控制台上看起来像这样

So the node that says "users" and "allUsers" looks like this on Firebase Console

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

'true' 部分只是为了让我可以将 bob@bob,com 放到数据库中……真正的部分永远不会用于任何事情.

The 'true' part was just so I could get the bob@bob,com onto the database...the true part will never be used for anything.

在登录 VC 时,老实说我不知道​​该怎么做

On the log in VC I honestly cannot figure out what to do

之前的回答说使用

hasChildren() 

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

And I used that and then googled what to do with that and I tried using something like this

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

How can I simply see if a textfield.text == an email already stored in firebase?

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

(I did convert the '.' to ',' when comparing)

推荐答案

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

Please don't use email addresses as keys. Email addresses are dynamic and may change (as in if the users wants to change it) and if they do, you'll have a mess on your hands as every node that directly used that email would have be deleted and re-created.

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

Best practice is to disassociate key's from the data they contain.

这是要使用的结构

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

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

then you simply query the email node for the email you are looking for, and handle accordingly if it exists.

还有一些代码

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

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

For completeness it would be a little more Swifty to use

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

这篇关于将 textfield.text 与 firebase 字符串 swift 进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆