查询Firebase中的现有节点是否返回null

我的树结构看起来像在FireBase中

  • 图书
  • USERS
    • UserA_ID
    • UserB_ID
    • UserC_ID
      • 用户名:@””
      • USEREMAIL:@ “”
      • UserBookReadCount:@ “”

在我去更新“BookReadCount”之前,我试着用下面的代码来查询这个用户的存在。 在里面

FQuery* query = [[userRef queryOrderedByKey] queryEqualToValue:userIDKey]; [query observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) { NSLog(@"Snapshot value: %@", snapshot.value); if(snapshot.hasChildren) { // UserX_ID exists. Use 'updateChildValues' to update the data } else { // UserX_ID does NOT exist. Use 'setValue' to create the data } }]; 

如果用户不存在,则使用“setValue”将新用户添加到此列表中。 如果用户存在,那么我使用'updateChildValues'来更新用户数据。 这一切工作正常。

我面临的问题是查询。 即使用户存在或不存在,上面的查询总是返回null。

任何想法明显,我失踪了?

几件事情。 要testingnull,你应该真的

  if (snapshot.value == [NSNull null]) 

这是一个包含NSNull检查代码的稍微修改的实现。

 Firebase *ref = [self.myRootRef childByAppendingPath:@"users"]; FQuery *query = [[ref queryOrderedByKey] queryEqualToValue:@"UserC_ID"]; [query observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) { if (snapshot.value == [NSNull null]) { NSLog(@"was null"); } else { NSLog(@"key: %@ %@", snapshot.key, snapshot.value); } }]; 

我build立了一个与您使用的结构相同的testingFirebase,它可以正常工作。 对不存在的用户标识返回null,并在返回时返回节点UserC_ID。

所以也许你的代码,设置userIDKey行为不当,或者你的userRef是不可靠的。