1

I have the following firebase data structure below:

-posts
    -postKey1
        +commentKey1
        +commentKey2
        +commentKey3

I need to get the number of comments for a particular post given a post key. So above data should return 3 for postKey1.

alltej
  • 6,787
  • 10
  • 46
  • 87

2 Answers2

1

As far as I am aware, there is no way to perform a count without downloading the data you wish to count.

Instead, you could use AngularFire2 to retrieve all of the data under postKey1 and could then count the retrieved posts. Or, if you are willing to put in the effort, you could use the REST API, specifying the shallow parameter to retrieve only the keys under postKey1 - which you could then count.

cartant
  • 57,105
  • 17
  • 163
  • 197
  • I have done that REST api that's why I ask specifically the angularfire2. I saw some examples in angularfire2 but getting errors. I end up adding some counter node/element but don't want to do that in all other nodes that requires count to be track. – alltej Feb 28 '17 at 01:33
  • There is nothing in the API that will give you a count. If you want to maintain a count yourself, look at http://stackoverflow.com/a/34092088/6680611 – cartant Feb 28 '17 at 02:03
  • I had it both ways. One where I maintain a count and one where I did the `numChildren`. But it becomes a repetitive tasks maintaining a count if I have to do it in all the entities/tables. For example, user count, post count, vote count, etc. I think the angularfire2 api needs an equivalent for the `shallow` parameter that it is available in the REST API – alltej Mar 06 '17 at 15:01
-1

The method to count the number of comments is called inside a loop in the html file.

...
<label>{{getCommentCount(post.$key)}} </label>

...

Here's the implementation which works for me. The numChildren() method did the trick. Also I think the preserveSnapshot seem to eliminate the errors I was having (like the infinite calls/get which results to insufficient resources errors in browser).

  getCommentCount(postKey: string) : number{
    let commentCount = 0;
    let url = `/posts/${postKey}`;
    const posts = this.af.database.object(url, { preserveSnapshot: true });
    posts.subscribe(snapshot => {
      commentCount = snapshot.numChildren();
    });
    return commentCount;
  }
alltej
  • 6,787
  • 10
  • 46
  • 87
  • Using the snapshot's `numChildren` to obtain a count will involve retrieving all of the data for that snapshot from the server. Also, the subscription is never unsubscribed, so the AngularFire2 `object` and its internal Firebase ref will live for the life of your application. And by calling `getCommentCount` from within the template, the function will be called upon each change detection. – cartant Mar 01 '17 at 04:34
  • For the solution I posted, I created a different table/node that does not have the content of the post and the comment but just an association of the post key and comment key. This way it is very light weight and no data payload. Probably not ideal. – alltej Mar 06 '17 at 15:04