Node.js 如何在不知道firebase和firebase admin中的密钥的情况下获取快照的子项

Node.js 如何在不知道firebase和firebase admin中的密钥的情况下获取快照的子项,node.js,firebase,firebase-realtime-database,firebase-admin,Node.js,Firebase,Firebase Realtime Database,Firebase Admin,我的数据库中有一个如下所示的队列: server queue -RANDOM_ID_1234 active: "true" text: "Some text" -RANDOM_ID_5678 active: "false" text: "Another text" -RANDOM_ID_91011 active: "false" text: "Text that does not matter"

我的数据库中有一个如下所示的队列:

server
  queue
    -RANDOM_ID_1234
      active: "true"
      text: "Some text"
    -RANDOM_ID_5678
      active: "false"
      text: "Another text"
    -RANDOM_ID_91011
      active: "false"
      text: "Text that does not matter"
/* 
 *  I get the keys of the object as array 
 *  and take the first one.
 */
const key = _.first(_.keys(snap.val())); 
/* 
 *  then I create a path to the value I want 
 *  using the key.
 */
const text= snap.child(`${key}/text`).val(); 
我想查询并获取活动项:

queueRef.orderByChild('active').equalTo('true').once('value', function(snap) {
  if (snap.exists()) {
    console.log(snap.val());
  }
});
console.log
将返回如下内容:

{
  -RANDOM_ID_1234: {
      active: "true"
      text: "Some text"
  }
}
我如何在不知道关键点的情况下获取文本


我使用了lodash(见下面我的答案),但肯定有更好的方法。我使用lodash并获得如下密钥:

server
  queue
    -RANDOM_ID_1234
      active: "true"
      text: "Some text"
    -RANDOM_ID_5678
      active: "false"
      text: "Another text"
    -RANDOM_ID_91011
      active: "false"
      text: "Text that does not matter"
/* 
 *  I get the keys of the object as array 
 *  and take the first one.
 */
const key = _.first(_.keys(snap.val())); 
/* 
 *  then I create a path to the value I want 
 *  using the key.
 */
const text= snap.child(`${key}/text`).val(); 
然后从快照中获取文本,如下所示:

server
  queue
    -RANDOM_ID_1234
      active: "true"
      text: "Some text"
    -RANDOM_ID_5678
      active: "false"
      text: "Another text"
    -RANDOM_ID_91011
      active: "false"
      text: "Text that does not matter"
/* 
 *  I get the keys of the object as array 
 *  and take the first one.
 */
const key = _.first(_.keys(snap.val())); 
/* 
 *  then I create a path to the value I want 
 *  using the key.
 */
const text= snap.child(`${key}/text`).val(); 

对Firebase数据库执行查询时,可能会有多个结果。因此,快照包含这些结果的列表。即使只有一个结果,快照也将包含一个结果列表

Firebase快照具有迭代其子项的内置方式:

queueRef.orderByChild('active').equalTo('true').once('value', function(snapshot) {
  snapshot.forEach(function(child) {
    console.log(child.key+": "+child.val());
  }
});

如果有类似于
getChildAtIndex(0)
或数组表示法
[0]
的东西,那就太棒了。