[英]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 '.' 由于未捕获的异常'InvalidPathValidation'而终止应用程序,原因:'(hasChild :)必须为非空字符串且不包含'。 '#' '$' '[' or ']''
'#''$''['或']''
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")
}
})
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 . 这样,您的密钥就是“电子邮件”,而您的价值就是theemail 。 In the value , every string is allowed.
在value中 ,允许每个字符串。
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{
}
})
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.