我正在尝试查询以查找与特定电子邮件匹配的用户。当我执行查询时,我收到了数据库中的所有电子邮件。我的最终目标是通过他或她的电子邮件找到此用户,然后更新InMatch
和SearchingForUser
键值。这是我的代码:
func fetchUsersSearchingForMatch() {
ref = FIRDatabase.database().reference()
let user = AppUser()
ref.child("users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
user.userEmail = (snapshot.value as? NSDictionary)?["email"] as? String
user.searchingForMatch = ((snapshot.value as? NSDictionary)?["SearchingForMatch"] as? Bool)!
print(dictionary)
if user.searchingForMatch == true {
self.users.append(user)
self.foundUser = self.users[0]
print("this is your foundUser \(self.foundUser.userEmail)")
print("this number of users are searching for a match: \(self.users.count)")
}
}
self.foundUserSearchingForMatch()
})
}
func foundUserSearchingForMatch() {
if user.inMatch == false {
let query = self.ref.child("users").queryOrdered(byChild: "email").queryEqual(toValue: foundUser.userEmail)
print("this is the query \(query)")
}
}
在我知道我选择合适的用户之前,我没有进行任何更新。当我这样做时,你如何更新这些值?我可以在查询中做到吗?
JSON TREE:
{"Firebase APP": {
"RANDOM USER ID": {
"USEREMAIL": "email@example.com",
"SearchingForMatch": "true",
"InMatch": "false" }
}
当用户找到匹配项时,我想将InMatch
键更改为true
,将SearchingForMatch
更改为false
。
我不想更新我的电子邮件或foundUser
电子邮件。我想更新foundUser
,InMatch
和SearchingForMatch
键值。
我从用户那里收到来自FBDB的匹配的电子邮件的方式是当我检查所有用户中的SearchingForMatch
是否等于true
时,如果它等于true
,我将它附加到一个数组,然后获取第一个元素的电子邮件(现在是foundUser
)。使用foundUser
电子邮件,我想更新该用户的关键值(InMatch
和SearchingForMatch
)。
答案 0 :(得分:1)
试试这个: -
FIRAuth.auth()?.currentUser?.updateEmail("new_Email", completion: { (Err) in
if Err == nil {
FIRDatabase.database().reference().child("Users").queryOrdered(byChild: "USEREMAIL").queryEqual(toValue: ).observeSingleEvent(of: .value, with: {(Snap) in
if let snapDict = Snap.value as? [String:AnyObject]{
for each in snapDict{
let key = each.key
let match = each.value["SearchingForMatch"] as! Bool
FIRDatabase.database().reference().child("Users/\(key)").updateChildValues(["SearchingForMatch" : "true"], withCompletionBlock: { (err, ref) in
if err == nil{
//Task acomplished....
}
})
}
}
})
}else{
//Couldn't update the auth value.
}
})