如何检查快照中是否包含Firebase中的特定值?

[英]How do i check if snapshot contains a specific value in Firebase?


When I run this code I get an error that says this:

当我运行此代码时,我收到一条错误,上面写着:

Terminating app due to uncaught exception 'InvalidPathValidation', reason: '(hasChild:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''

由于未捕获的异常'InvalidPathValidation'而终止应用程序,原因:'(hasChild :)必须是非空字符串且不包含'。' '#''$''['或']''

My problem is how do I check if my snapshot contains the value user.email?

我的问题是如何检查我的快照是否包含值user.email?

self.databaseRef.child("Test").child("xxx").observeSingleEvent(of: .value, with: { (snapshot) in

                    if snapshot.hasChild((user!.email)!){
                        print("yess")
                    }else{
                        print("Not in db")
                    }


                })

2 个解决方案

#1


1  

You specify your problem in your text. An email always contains a dot '.'. That is not allowed in the key. You check if the key contains the email, but that is not allowed. Structure your database like this:

您在文本中指定了问题。电子邮件始终包含一个点'。'。密钥中不允许这样做。您检查密钥是否包含电子邮件,但不允许这样做。像这样构建数据库:

"email": theemail

This way your key is "email" and your value is theemail. In the value, every string is allowed.

这样你的密钥就是“电子邮件”,你的价值就是电子邮件。在值中,允许每个字符串。

self.databaseRef.child("Test").child("xxx").observeSingleEvent(of: .value, with: { (snapshot) in
    let values = snapshot.value as? NSDictionary
    let email = values?["email"] as? String ?? "No email found"
    // OR
    if let email = values?["email"] as? String{
    //email (above declared variable) holds the value (the real email)
    }else{
    //no email
    }
    if email == (user!.email)!{

    }else{
    }
})

#2


0  

Let me know if this works. It checks to see if the email field actually exists. I think you are getting the error because of the way you're checking for the actual user's email.

让我知道这个是否奏效。它会检查电子邮件字段是否确实存在。我认为您正在收到错误,因为您正在检查实际用户的电子邮件。

self.databaseRef.child("Test").child("xxx").observeSingleEvent(of: .value, with: { (snapshot) in

    if snapshot.hasChild("email"){

        print("yess")

    }else{

        print("Not in db")
    }
})

hasChild returns true if the specified child path has (non-null) data.

如果指定的子路径具有(非空)数据,则hasChild返回true。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2017/08/02/6f7f0828cfb4004e989b53fe3a195450.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号