Ad

Cannot Get Nested Child's Data From Firebase?

I am trying to get all the reviews for the post with a specific ID. I have tried all the solutions but it just never works. The same method works when I am trying to get "posts" (1 st level node) , but nested levels are not working

db: enter image description here

 calculate_post_rating(post_id){

    console.log('this post id: '+post_id);

    let dbref = firebase.database().ref('/user-reviews/'+ post_id + '/');

    dbref.on('child_added', function (data){                

        console.log(data.key); //console is not even printing this, seems like this part is not even executed.

        console.log('rated by user: ' + data.val().rating);

 });
Ad

Answer

It would be easier if you structure your data differently and avoid key inside key:

https://firebase.google.com/docs/database/rest/structure-data#how_data_is_structured_its_a_json_tree

users-reviews: 
 - reviewingUserId 
   - comment: 'Wow'
   - rating: 5
   - subject: 'This may help you out'
Ad
source: stackoverflow.com
Ad