Firebase Functions error: .once is not a function

被刻印的时光 ゝ 提交于 2021-02-08 21:47:10

问题


I am trying to deploy a simple function to Firebase, but I am having some difficulties. Every time I try to use .once on a reference Firebase tells me that it is not a function. Here is my code

exports.testFunction = functions.database.ref('/Rooms/{firstID}/{pushId}/On').onWrite(event => {
  const value = event.data.val();
  var ref = functions.database.ref(roomNum);
  return ref.once('value').then(snapshot => {
    console.log(snapshot.numChildren);
    return true;
  }); });

I have also tried the following:

firebaseRef.once('value', function(dataSnapshot) {
  console.log(snapshot.numChildren);
});

Nothing seems to work. Does anyone know of a fix or a different way of getting the number of children from a ref/snapshot?

Thank you.


回答1:


functions.database.ref is a different object than the one you're used to using on the client. It's sole purpose is to listen for writes using it's only function, onWrite.

You can obtain your intended ref thru the event parameter.

var ref = event.data.ref

This is a reference to the path you specified in onWrite.

If you want the root reference:

var rootRef = event.data.ref.root

Further reading: https://firebase.google.com/docs/reference/functions/functions.database



来源:https://stackoverflow.com/questions/44574157/firebase-functions-error-once-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!