Firebase snapshot.key 没有返回实际密钥?
Firebase snapshot.key not returning actual key?
我有一个根据用户 ID 搜索用户的查询。
usersRef.queryOrderedByChild("email").queryEqualToValue(email).observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
print("user exists")
print(snapshot.key)
查询 returns 正确的用户,但行 print(snapshot.key)
字面上 returns 单词 "users",而不是实际的用户 ID。 print(snapshot)
returns 以下用户:
Snap (users) {
DELyncz9ZmTtBIKfbNYXtbhUADD2 = {
email = "test3@gmail.com";
"first_name" = test;
"last_name" = test;
};
如何获得DELyncz9ZmTtBIKfbNYXtbhUADD2
?我可以使用 let email = child.value["email"]
获取电子邮件,但我无法获取密钥,因为它不是命名属性。
谢谢!!
编辑:由于 Frank 的回答更新了代码。获取 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)
当您 运行 在某个位置进行查询时,结果将是匹配 children 的列表。即使只有一个匹配项,结果也将是一个 child.
的列表
您正在打印所有结果 children 的密钥。由于没有单一的结果,SDK打印出你查询的location/collection的key:users
.
您可能要寻找的是遍历匹配的 children 并打印它们的密钥:
let query = usersRef.queryOrderedByChild("email").queryEqualToValue(email)
query.observeEventType(.Value, withBlock: { snapshot in
for child in snapshot.children {
print(child.key)
}
})
我有一个根据用户 ID 搜索用户的查询。
usersRef.queryOrderedByChild("email").queryEqualToValue(email).observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
print("user exists")
print(snapshot.key)
查询 returns 正确的用户,但行 print(snapshot.key)
字面上 returns 单词 "users",而不是实际的用户 ID。 print(snapshot)
returns 以下用户:
Snap (users) {
DELyncz9ZmTtBIKfbNYXtbhUADD2 = {
email = "test3@gmail.com";
"first_name" = test;
"last_name" = test;
};
如何获得DELyncz9ZmTtBIKfbNYXtbhUADD2
?我可以使用 let email = child.value["email"]
获取电子邮件,但我无法获取密钥,因为它不是命名属性。
谢谢!!
编辑:由于 Frank 的回答更新了代码。获取 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)
当您 运行 在某个位置进行查询时,结果将是匹配 children 的列表。即使只有一个匹配项,结果也将是一个 child.
的列表您正在打印所有结果 children 的密钥。由于没有单一的结果,SDK打印出你查询的location/collection的key:users
.
您可能要寻找的是遍历匹配的 children 并打印它们的密钥:
let query = usersRef.queryOrderedByChild("email").queryEqualToValue(email)
query.observeEventType(.Value, withBlock: { snapshot in
for child in snapshot.children {
print(child.key)
}
})