gpt4 book ai didi

javascript - Firebase Functions 错误 : . once is not a function

转载 作者:搜寻专家 更新时间:2023-10-31 23:37:06 25 4
gpt4 key购买 nike

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

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

似乎没有任何效果。有谁知道从引用/快照中获取 child 数量的修复方法或不同方法?

谢谢。

最佳答案

functions.database.ref 是一个不同于您在客户端上使用的对象的对象。它的唯一目的是使用它唯一的函数 onWrite 来监听写入。

您可以通过 event 参数获取您想要的 ref。

var ref = event.data.ref

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

如果你想要根引用:

var rootRef = event.data.ref.root

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

关于javascript - Firebase Functions 错误 : . once is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44574157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com