Firebase snapshot.key不能返回实际的密钥? [英] Firebase snapshot.key not returning actual key?
问题描述
我有一个基于用户ID搜索用户的查询。
usersRef.queryOrderedByChild(email)。queryEqualToValue(email).observeEventType(.Value,withBlock:{snapshot in
如果snapshot.exists(){
print(user exists)
print(snapshot.key)
查询返回正确的用户,但是 print(snapshot.key)
从字面上返回单词users,而不是实际的用户ID 打印(快照)
返回以下用户:
(用户){
DELyncz9ZmTtBIKfbNYXtbhUADD2 = {
email =test3@gmail.com;
first_name= test;
last_name= test;
} ;
如何获得 DELyncz9ZmTtBIKfbNYXtbhUADD2
?I可以通过使用 let email = child.value [email]
来获取电子邮件,但是我不能得到密钥,因为它不是一个已命名的属性。 b
$ b
谢谢!!
编辑:感谢Frank的回答,更新了代码。获取不明确使用键
query.observeEventType(.Value,withBlock :{快照
print(snapshot.key)
如果snapshot.exists(){
print(user exists)
for child在snapshot.children {
print(child.key)
在某个位置运行查询时,结果将是匹配子项的列表。即使只有一个匹配的项目,结果也会是一个孩子的列表。
您正在打印所有子项的密钥。由于没有单一的结果,SDK会打印出您查询的位置/集合的关键字: users
。
您可能要查找的是循环匹配的子项并打印其键:
let query = usersRef.queryOrderedByChild (email)。queryEqualToValue(email)
query.observeEventType(.Value,withBlock:{snapshot in
for snapshot.children {$ b $ print(child.key)
}
})
I have a query that searches for a user based on user ID.
usersRef.queryOrderedByChild("email").queryEqualToValue(email).observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
print("user exists")
print(snapshot.key)
The query returns the correct user, but the line print(snapshot.key)
literally returns the word "users", and not an actual user ID. print(snapshot)
returns the following user:
Snap (users) {
DELyncz9ZmTtBIKfbNYXtbhUADD2 = {
email = "test3@gmail.com";
"first_name" = test;
"last_name" = test;
};
How can I get DELyncz9ZmTtBIKfbNYXtbhUADD2
? I can get the email by using let email = child.value["email"]
but I can't get the key because it's not a named attribute.
Thanks!!
EDIT: Updated code thanks to Frank's answer. Getting ambiguous use of key
query.observeEventType(.Value, withBlock: { snapshot in
print(snapshot.key)
if snapshot.exists() {
print("user exists")
for child in snapshot.children {
print(child.key)
When you run a query at a location, the result will be a list of the matching children. Even if there is only a single matching item, the result will be a list of one child.
You're printing the key of all resulting children. Since there is no single result, the SDK prints the key of the location/collection that you queried: users
.
What you're likely looking for is to loop over the matching children and print their keys:
let query = usersRef.queryOrderedByChild("email").queryEqualToValue(email)
query.observeEventType(.Value, withBlock: { snapshot in
for child in snapshot.children {
print(child.key)
}
})
这篇关于Firebase snapshot.key不能返回实际的密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!