如何通过firebase中的嵌套值获取键名?

时间:2015-01-30 06:12:28

标签: ios objective-c firebase livechat firebasesimplelogin

enter image description here

我的firebase以这种方式建模。我想编写一个查询来获取userID(simplelogin:1),其中email是“test@gmail.com”。

在这里,我想获得密钥:“simplelogin:1”,用它的电子邮件地址搜索:“test@gmail.com”

我正在使用适用于iOS的firebase SDK。有人可以建议我查询吗?在Javascript或Objective-C

2 个答案:

答案 0 :(得分:9)

在一点点搞乱之后,我找到了问题的答案:

Firebase *ref = [_rootReference childByAppendingPath:@"Users"];

[[[ref queryOrderedByChild:@"Email"]queryEqualToValue:@"test@gmail.com" ]
 observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
     NSLog(@"%@ Key %@ Value", snapshot.key,snapshot.value);
 }];

snaphot.key将是我想要的值simplelogin:1。

答案 1 :(得分:1)

我只想在你的代码Prajeet上添加一个东西。我认为FEventType应该是 FEventTypeValue ,因为你每次添加孩子时都不会“观察”它。

Firebase *ref = [_rootReference childByAppendingPath:@"Users"];

[[[ref queryOrderedByChild:@"Email"]queryEqualToValue:@"test@gmail.com" ]
 observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
     NSLog(@"%@ Key %@ Value", snapshot.key,snapshot.value);
 }];