查询Firebase中的现有节点返回null [英] Querying for existing nodes in Firebase is returning null

查看:146
本文介绍了查询Firebase中的现有节点返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的树状结构在FireBase中显示

My tree structure looks like in FireBase


  • BOOKS

  • USERS


    • UserA_ID

    • UserB_ID

    • UserC_ID


      • 用户名:@

      • UserEmail:@

      • UserBookReadCount:@

      • BOOKS
      • USERS
        • UserA_ID
        • UserB_ID
        • UserC_ID
          • UserName:@""
          • UserEmail:@""
          • UserBookReadCount:@""

          在我去更新'BookReadCount'之前,我尝试使用以下代码查询此用户的存在。在

          Before I go and update the 'BookReadCount', I try to query for the existence of this user using the following code. In the

          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'来更新用户数据。这一切都很好。

          If the user does not exist I then use 'setValue' to add a new user to this list. If the user exists then I use 'updateChildValues' to update the users data. All this is working fine.

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

          The problem I'm facing is with the queries. Even if the user exists or doesn't exist, the above query always returns null.

          对于我遗漏的明显事物的任何想法?

          Any thoughts on something obvious that I'm missing ?

          推荐答案

          有几件事。要测试null,你应该

          A couple of things. To test for null you should really

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

          这是一个略微修改的代码实现,包括NSNull检查。

          Here's a slightly modified implementation of your code that includes the NSNull check.

          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);
                  }
              }];
          

          我设置了一个测试Firebase,其结构与您使用的相同,并且工作正常;对于不存在的用户id返回null,并返回节点UserC_ID。

          I set up a test Firebase with the same structure you are using and it works correctly; returns null for nonexistent user id's and returns the node UserC_ID when it does.

          所以也许你设置userIDKey的代码行为不端或者你的userRef可能不稳定。

          So maybe your code that sets the userIDKey is misbehaving or perhaps your userRef is wonky.

          这篇关于查询Firebase中的现有节点返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆