This question already has an answer here:
How to read any data from database on its write trigger on one of its child in firebase functions?
                                
                                    (1个答案)
                                
                        
                                9个月前关闭。
            
                    
我正在使用Firebase实时数据库,并试图在名为ignore的字段添加到数据库中时创建触发器。但是我首先要访问数据库中的另一个数据,并在函数中使用if。我的功能如下:

exports.dbTest2 = functions.database.ref('/{uid}/ignore')
  .onCreate((snapshot, context) => {

    const uid = context.params.uid
    console.log(`Current User ${uid}`)

    // Data added at the location
    const ignoreData = snapshot.val()
    const endTime = new Date(ignoreData.endTime).toString()

    const scheduleRef = snapshot.ref.parent.child('schedule')
    console.log(scheduleRef)

    let scheduleArr = [ /*something*/ ]
    return snapshot.ref.parent.child('schedule').set(scheduleArr)

  });


和我的数据库看起来像这样:

javascript - 如何在云端功能中访问Firebase数据库中的其他数据?-LMLPHP

从我当前的参考路径{uid}/ignore中,我想要获取schedule参考中的第一个元素(在图中圈出)。在上面的代码中,我尝试控制台记录引用,但是似乎没有这种方法可以从该对象获取值。

如何在云端功能中使对象在图像中圈出?

最佳答案

尝试像这样嵌套functions.database.ref

exports.dbTest2 = functions.database.ref('/{uid}/ignore')
    .onCreate((snapshotIgnore, context) => {

      const uid = context.params.uid;

      admin.database().ref(`/${uid}/schedule`).once('value', snapshotSchedule => {
        console.log(snapshotSchedule.val()[0]);
      });
    });

09-26 19:02
查看更多