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