Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase:如何按键查询?_Firebase_Firebase Realtime Database - Fatal编程技术网

Firebase:如何按键查询?

Firebase:如何按键查询?,firebase,firebase-realtime-database,Firebase,Firebase Realtime Database,我有一个要查询的位置表。钥匙是“050PNS74ZW8XZ” 正如你所看到的,它是存在的 但是,当我打印出任何属性时,它都不起作用 function testing(req, resp) { const location = database.ref('locations').equalTo('050PNS74ZW8XZ'); console.log('location:', location.account_capabilities); // <--- prints undef

我有一个要查询的位置表。钥匙是“050PNS74ZW8XZ”

正如你所看到的,它是存在的

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

function testing(req, resp) {
  const location = database.ref('locations').equalTo('050PNS74ZW8XZ');
  console.log('location:', location.account_capabilities); // <--- prints undefined
  resp.json("asfdsf");
}
功能测试(请求、响应){
const location=database.ref('locations').equalTo('050PNS74ZW8XZ');

console.log('location:',location.account_capabilities);//如果您知道可以直接访问子项的密钥,则将其转换为带有$firebaseObject的对象

var locref = database.ref('locations').child('050PNS74ZW8XZ');
const location = $firebaseObject(locref);

从列表中查询时应使用equalTo函数。

如果您知道键,则可以使用
child
方法

但是,更根本的问题是该值不能直接从ref获得。您可以使用
once
方法获取该值,以侦听
value
事件,该事件返回解析为快照的承诺:

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);
}
更多信息请参见。

关于

const location = database.ref('locations/050PNS74ZW8XZ');

这是因为“location”是firebase引用而不是对象。您必须使用$firebaseobjectis,这是firebase引用上的方法吗?很抱歉,我给出了一个角度解决方案,cartant的解决方案是纯javascript的