如何在Cloud Function中获取firebase数据库中节点的值

时间:2017-12-06 01:24:53

标签: javascript firebase firebase-realtime-database google-cloud-functions

exports.respublished = 
functions.database.ref('/Posts/{postid}').onWrite(event => {
  const snapshot = event.data;
  const postid = event.params.examid;
  const uid = snapshot.child('uid').val();
  const ispublic = snapshot.child('Public').val();
  firebase.database.ref('Users/' + uid).once(event => {
    const snapshot = event.data;
    const name = snapshot.child('name').val();
  });
});

该事件由另一个节点触发,我想从firebase数据库的另一个节点中检索数据。我已经尝试了上面的代码,但它产生了一个错误,说明TypeError:firebase.database.ref(...)。一次不是函数。

1 个答案:

答案 0 :(得分:0)

是的,我得到了我们可以使用此代码的答案

exports.respublished = functions.database.ref('/Posts/{postid}').onWrite(event => {
  const snapshot = event.data;
  const postid = event.params.examid;
  const uid = snapshot.child('uid').val();
  const ispublic = snapshot.child('Public').val();
  return admin.database().ref('Users/' + uid).once(event => {
    const snapshot = event.data;
    const name = snapshot.child('name').val();
  });
});