0

i want to to update child value depending on categoryId. I tried following this tutorial Firebase DB - How to update particular value of child in Firebase Database. It’s work, but it’s not storing in the same ref. It’s store in another ref.

https://i.stack.imgur.com/SaqjD.png

firebase.database().ref('usuario')
                    .on('value',event =>{
                        event.forEach(user =>{
                            user.child('eventos').forEach(evento =>{
                                if (evento.val().categoryId === payload.id){
                                  //Here is where i try to update the childe value, in my case category
                                    let ref = firebase.database().ref('usuario/'+user.key+'/'+evento.key+'/'+evento.val().category)
                                        .set(payload.name)
                                    console.log(ref)
                                }

                            })
                        });

                    });



Mohamed Mamun
  • 211
  • 3
  • 15

1 Answers1

1

2 problems: 1.You forgot to add "\eventos" on you child path. 2.dont use .set(), because it will delete all the other data. Instead of .set() use .update(). Try this code:

firebase.database().ref('usuario')
                    .on('value',event =>{
                        event.forEach(user =>{
                            user.child('eventos').forEach(evento =>{
                                if (evento.val().categoryId === payload.id){
                                  //Here is where i try to update the childe value, in my case category
                                    let ref = firebase.database().ref('usuario/'+user.key+'/eventos/'+evento.key+'/'+evento.val().category)
                                        .update(payload.name)
                                    console.log(ref)
                                }

                            })
                        });

                    });

Let me know if it still dont work

israel
  • 73
  • 1
  • 12