win*_*ton 3 firebase swift firebase-realtime-database swift2
我有一个查询,根据用户ID搜索用户.
usersRef.queryOrderedByChild("email").queryEqualToValue(email).observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
print("user exists")
print(snapshot.key)
Run Code Online (Sandbox Code Playgroud)
查询返回正确的用户,但该行print(snapshot.key)字面上返回单词"users",而不是实际的用户ID.print(snapshot)返回以下用户:
Snap (users) {
DELyncz9ZmTtBIKfbNYXtbhUADD2 = {
email = "test3@gmail.com";
"first_name" = test;
"last_name" = test;
};
Run Code Online (Sandbox Code Playgroud)
我该怎么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)
Run Code Online (Sandbox Code Playgroud)
在某个位置运行查询时,结果将是匹配子项的列表.即使只有一个匹配项,结果也会是一个孩子的列表.
您正在打印所有生成的孩子的钥匙.由于没有单个结果,SDK会打印您查询的位置/集合的密钥:users.
你可能想要的是循环匹配的孩子并打印他们的钥匙:
let query = usersRef.queryOrderedByChild("email").queryEqualToValue(email)
query.observeEventType(.Value, withBlock: { snapshot in
for child in snapshot.children {
print(child.key)
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2849 次 |
| 最近记录: |