Javascript snapshot.numChildren不是实时数据库触发器中的函数
我在Firebase应用程序中使用谷歌云功能。 我有一个方法来统计帖子中的评论,并更新帖子中名为Javascript snapshot.numChildren不是实时数据库触发器中的函数,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,我在Firebase应用程序中使用谷歌云功能。 我有一个方法来统计帖子中的评论,并更新帖子中名为comments\u count的属性 在我升级firebase控制台及其附属设备之前,它一直工作正常。现在,在日志中说,commentSnapshot.numChildren不是一个函数 函数中的代码如下所示: //Function that updates comments count inside post exports.setCommentsCount = functio
comments\u count
的属性
在我升级firebase控制台及其附属设备之前,它一直工作正常。现在,在日志中说,commentSnapshot.numChildren不是一个函数
函数中的代码如下所示:
//Function that updates comments count inside post
exports.setCommentsCount =
functions.database.ref('/Comments/{post_id}').onWrite((commentSnapshot, context) => {
const post_id = context.params.post_id;
const commentsCount = commentSnapshot.numChildren();
//rest of code here
}
您应该熟悉Firebase SDK用于实时数据库触发器的1.0版云函数中出现的问题 触发器不再接收DeltaSnapshot作为您提供的函数的第一个参数。现在它是一个对象,具有
before
和after
属性,每个属性都是一个对象。此DataSnapshot有一个numChildren方法:
functions.database.ref('/Comments/{post_id}').onWrite((change, context) => {
const post_id = context.params.post_id;
const commentsCount = change.after.numChildren();
}
我遇到了同样的错误。出现此错误是因为
numChildren()
是DataSnapshot接口,但snapshot不是DataSnapshot。
以下是正确的代码:
'use strict';
const functions = require('firebase-functions');
const MAX_USERS = 10;
exports.truncate = functions.database.ref('/chat').onWrite((change) => {
const parentRef = change.after.ref;
const snapshot = change.after
if (snapshot.numChildren() >= MAX_USERS) {
let childCount = 0;
const updates = {};
snapshot.forEach((child) => {
if (++childCount <= snapshot.numChildren() - MAX_USERS) {
updates[child.key] = null;
}
});
// Update the parent. This effectively removes the extra children.
return parentRef.update(updates);
}
return null;
});
“严格使用”;
const functions=require('firebase-functions');
const MAX_USERS=10;
exports.truncate=functions.database.ref('/chat').onWrite((change)=>{
const parentRef=change.after.ref;
const snapshot=change.after
if(snapshot.numChildren()>=最大用户数){
让childCount=0;
常量更新={};
snapshot.forEach((子项)=>{
如果(++childCount经过近一个小时的搜索,就这样了。非常感谢