本文介绍了Firebase函数错误:.once不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向Firebase部署一个简单的功能,但是遇到了一些困难.每次我尝试在参考Firebase上使用.once时,都会告诉我它不是函数.这是我的代码

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;
  }); });

我也尝试了以下方法:

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?

谢谢.

推荐答案

functions.database.ref 是一个与您以前在客户端上使用过的对象不同的对象.唯一的目的是使用唯一的函数 onWrite 监听写操作.

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.

您可以通过 event 参数获取所需的参考.

You can obtain your intended ref thru the event parameter.

var ref = event.data.ref

这是对您在 onWrite 中指定的路径的引用.

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

如果要使用根引用:

var rootRef = event.data.ref.root

进一步阅读: https://firebase.google.com/docs/reference/functions/functions.database

这篇关于Firebase函数错误:.once不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 21:29