Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript snapshot.numChildren不是实时数据库触发器中的函数_Javascript_Firebase_Firebase Realtime Database_Google Cloud Functions - Fatal编程技术网

Javascript snapshot.numChildren不是实时数据库触发器中的函数

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

我在Firebase应用程序中使用谷歌云功能。 我有一个方法来统计帖子中的评论,并更新帖子中名为
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经过近一个小时的搜索,就这样了。非常感谢