【问题标题】:Firebase query returns empty object from node but not APIFirebase 查询从节点返回空对象,但不返回 API
【发布时间】:2018-07-08 00:35:00
【问题描述】:

我有一个结构有点像的数据库

  - game
      - 1000
          - clues: ...
          - grid: ...
          - gid: 1000
          - createTime: ...
          - pid: 2780
       - 1001
          - clues: ... 
          - ...

我想检索 pid 为 2780 的所有游戏。我在 game 上为 pid 设置了 .indexOn 规则。在节点中,我目前正在做:

db.ref('game').orderByChild('pid').equalTo(this.pid).once('value').then(
        gameSnap => {
            console.log(gameSnap.hasChildren());  // false
            console.log(gameSnap.val()); // null
                gameSnap.forEach(
                    childSnap => {
                        console.log(childSnap); // no printout
                        this.setState(prevState => {
                            games: [...prevState.games, [childSnap.key, gameSnap.child("createTime").val()]]
                        });
                    });
        }
    );

gameSnap 始终为空。相比之下,当我使用 python Pyrebase 客户端通过 REST API 对数据库执行相同的查询时,会得到不同的结果:

In [257]: len(db.child('game').order_by_child('pid').equal_to(2780).get().each())
Out[257]: 25

我可能做错了什么?

【问题讨论】:

  • 您确定this.pid 是您期望的值吗?只是问,因为您在 python 示例中硬编码了值。
  • 你检查 this.pid 是否正确?
  • 我在发布之前删除了console.log(this.pid),这是正确的 IIRC,但我会尝试对其进行硬编码。
  • 试试console.log(childSnap.id + " is id and val is " + childSnap.val());
  • sljkhasdkljhskljhasdf this.pid"2780",它不是 2780,所以它打印正确但在数据库中找不到。

标签: javascript node.js firebase firebase-realtime-database


【解决方案1】:

原来this.pid"2780",它不是2780。所以添加parseInt 解决了我的问题。

【讨论】:

    猜你喜欢
    • 2019-01-07
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 2014-07-17
    相关资源
    最近更新 更多