Iterate through Firebase Sub-Array with Alphanumeric Ids

Viewed 50

I use post request to store data in the firebase realtime-database as with the push() in JavaScript. Previously my data was imported in the JSON format with comments ordered by numeric ids. This was very helpful to do some computation on the data. Now after shifting to post to allow users submit their comments and ratings of a particular item, I could not perform the same operations because the ids of the comments are now alphnumeric strings. What I got is number of comments undefined and average rating is undefined.

this.http.post(baseURL + 'dishes/' + this.dish.id + '/comments.json', this.comment)
  .subscribe(res => {
    console.log(res);
  })

I am performing some calculations:

//this.dish.comments.push(this.comment);
this.numcomments = this.dish.comments.length;
this.dish.comments.forEach(comment => total += comment.rating);
this.avgstars = (total / this.numcomments).toFixed(2);

And they are no longer available. What I got is comments: undefined and averagestars: undefined.

Here is a sample of my data: enter image description here

Any idea how to iterate through the comments array with its alphanumeric ids to get to the values and render them to the UI properly upon calculation?

I welcome your proposals and corrections.

Thanks,

0 Answers
Related