Firebase:如何按键查询?

big*_*ato 8 firebase firebase-realtime-database

我有一个我想查询的位置表.关键是"050PNS74ZW8XZ".

在此输入图像描述

如你所见,它存在.

但是,当我打印出任何属性时,它不起作用

function testing(req, resp) {
  const location = database.ref('locations').equalTo('050PNS74ZW8XZ');
  console.log('location:', location.account_capabilities); // <--- prints undefined
  resp.json("asfdsf");
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么???

car*_*ant 10

如果您知道密钥,则可以使用该child方法.

然而,更基本的问题是该值不能直接从ref获得.您可以使用once方法获取值- 侦听value事件 - 返回解析为快照的promise:

function testing(req, resp, next) {
  database.ref('locations').child('050PNS74ZW8XZ')
    .once('value')
    .then(function(snapshot) {
      var value = snapshot.val();
      console.log('location:', value.account_capabilities);
      resp.json(value.account_capabilities);
    })
    .catch(next);
}
Run Code Online (Sandbox Code Playgroud)

文档中有更多信息.