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

时间:2018-05-05 18:25:57

标签: javascript firebase firebase-realtime-database google-cloud-functions

我在Firebase应用中使用谷歌云功能。 我有一种方法来计算帖子中的评论,并在帖子中更新名为comments_count的属性。

在升级firebase控制台及其依赖项之前,它工作正常。现在,在日志中它说commentSnapshot.numChildren is not a function

函数中的代码如下所示:

  //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
  }

2 个答案:

答案 0 :(得分:4)

您应该熟悉1.0版本的Fire Functions SDK for Realtime Database触发器中发生的breaking changes

onWrite触发器不再接收DeltaSnapshot作为您提供的函数的第一个参数。它现在是Change对象,具有functions.database.ref('/Comments/{post_id}').onWrite((change, context) => { const post_id = context.params.post_id; const commentsCount = change.after.numChildren(); } export class NavigationComponent implements OnInit { constructor(public globals: Globals, public auth: AuthService, public placeService: PlaceService) { auth.handleAuthentication(); if (auth.isAuthenticated) { console.log(' userName ' + this.firstName + ' userName ' + this.userName + ' dp ' + this.dpUrl); this.fetchProfile(); console.log(' userName ' + this.firstName + ' userName ' + this.userName + ' dp ' + this.dpUrl); } } ngOnInit() { this.getCountries(); } private firstName: any; private userName: any; private dpUrl: any; private fetchProfile() { this.auth.getProfileReponse(this.processResponse); } public processResponse(userResponse: Observable<HttpResponse<UserResp>>) { userResponse.subscribe(resp => { const user: User = { ...resp.body }.flatUser; console.log('user'); console.log(user); // printing user perfetcly console.log(user.firstName); // printing user perfetcly this.firstName = user.firstName; // facing error here this.userName = user.userName; this.dpUrl = user.dpURL; // console.log(this.flatUser); }); } } 属性,每个属性都是DataSnapshot个对象。此DataSnapshot具有numChildren方法:

core.js:2090 ERROR TypeError: Cannot set property 'firstName' of undefined
at SafeSubscriber.eval [as _next] (navigation.component.ts:83)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:240)
at SafeSubscriber.next (Subscriber.js:187)
at Subscriber._next (Subscriber.js:128)

答案 1 :(得分:0)

我遇到了同样的错误。由于numChildren()是一个DataSnapshot接口,但快照不是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;
});