作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试将一个简单的函数部署到 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/
我是一名优秀的程序员,十分优秀!