使用Firebase和Swift从查询中获取信息 [英] Grabbing information out of a query with Firebase and Swift
问题描述
var usernameEntered = self.textField.text!
var databaseReferenceQuery = DataService.ds.REF_USERS.queryOrderedByChild(username)。queryEqualToValue(usernameEntered).observeSingleEventOfType(.Value,
withBlock:{(snapshot)in
if (snapshot.value是NSNull){
} else {
print(snapshot.key)
print(snapshot.value。)
}
} ,withCancelBlock:{(error)in
//发生错误
})
}
这是我得到的。
我的firebase数据库设置如下:
用户
20394802938lsjfkjaweli(the uid)
用户名:Bob
0skjflakjlkwjf93j09
用户名:Bobby
通过运行该查询,我正在搜索用户名并检查它是否等于输入到文本字段中的内容。 b
$ b
当我做一些像print(snap.value)的时候,我得到了username:(无论我如果它匹配一个用户名,就把它放到文本字段中)
我需要的是能够截取与该用户名关联的用户ID。
使用snapshot.key为我提供了用户。
对于snapshot.children中的child,let key = child.key as
print(this key \(key))
获取返回的快照并将变量键设置为子键,在本例中为UID,
var usernameEntered = self.textField.text!
var databaseReferenceQuery = DataService.ds.REF_USERS.queryOrderedByChild("username").queryEqualToValue(usernameEntered).observeSingleEventOfType(.Value,
withBlock: { (snapshot) in
if ( snapshot.value is NSNull ) {
} else {
print(snapshot.key)
print(snapshot.value.)
}
}, withCancelBlock: { (error) in
// An error occurred
})
}
This is what I've got.
My firebase database is set up like :
Users
20394802938lsjfkjaweli (the uid)
username: Bob
0skjflakjlkwjf93j09
username: Bobby
By running that query I'm searching through the usernames and checking to see if it equals what's entered into the text field.
When I do something like print(snap.value) I get "username: (whatever i put into the text field if it matches a username)
What I need is to be able to snag the user ID associated with that username.
Using snapshot.key provides me with "Users". How do I get one level below that?
for child in snapshot.children {
let key = child.key as String
print("this key \(key)")
Takes the returned snapshot and sets the variable key to the child's key, which in this case in the UID,
这篇关于使用Firebase和Swift从查询中获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!