Volodymyr Humeniuk
Volodymyr Humeniuk

Reputation: 3791

Length of objects in Firebase list

I have next Firebase database structure:

posts
    | 
    post
       |
       comments
              | comment 1
              | comment 2

This is a collection of posts, each post has a collection of comments. I want to output in the post the number of comments:

<ng-container *ngIf="post$ | async; let post">
    {{ post.comments?.length }}
</ng-container>

But this does not work. What am I doing wrong?

Upvotes: 1

Views: 2261

Answers (1)

balsick
balsick

Reputation: 1201

Are you sure that the one you wrote is the way Firebase is saving your data? They cannot save lists as array, but they usually convert them to maps having as key the index.

So, probably, your post.comments is not an array but a map. Try use size instead of length. If it is not a map but an object (I'm not sure about this), you should use Object.keys(post.comments).size instead.

Upvotes: 3

Related Questions