Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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
Node.js firebase函数检索数据_Node.js_Firebase_Firebase Realtime Database - Fatal编程技术网

Node.js firebase函数检索数据

Node.js firebase函数检索数据,node.js,firebase,firebase-realtime-database,Node.js,Firebase,Firebase Realtime Database,我在firebase上有这样的数据结构 -B4PWzQ84KUOeuTuxVY:{ doctorId:2, Message:abc } 我想在StudentId的基础上检索消息,因为有这么多记录 我的代码只检索整个对象,但我只需要doctorId为2的消息值 db.ref('students/' ) .orderByChild("DoctorId") .equalTo("2") .once('value',(whereresult) =&

我在firebase上有这样的数据结构

-B4PWzQ84KUOeuTuxVY:{

doctorId:2,
Message:abc
}
我想在StudentId的基础上检索消息,因为有这么多记录 我的代码只检索整个对象,但我只需要doctorId为2的消息值

   db.ref('students/' )
        .orderByChild("DoctorId")
        .equalTo("2")
        .once('value',(whereresult) => {
        var message = whereresult.val();
 });

代码中有三个问题:

  • 属性名称区分大小写,因此
    doctorId
    doctorId
  • 您将ID存储为一个数字,因此应该将一个数字传递给
    equalTo
  • 对Firebase数据库执行查询时,可能会有多个结果。因此,快照包含这些结果的列表。即使只有一个结果,快照也将包含一个结果列表
  • 总计:

    db.ref('students/' )
      .orderByChild("doctorId")
      .equalTo(2)
      .once('value',(snapshot) => {
          snapshot.forEach(function(child) {
              console.log(child.key, child.val());
          });
      });
    

    代码中的DoctorId应该是带小写字母的DoctorId,并且值应该是一个数字。所以
    db.ref('students/').orderByChild(“doctorId”).equalTo(2).once('value',(whereresult)=>{var message=whereresult.val();