问题
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