Mohamed Mamun
Mohamed Mamun

Reputation: 299

Firebase how to update child value in javasscript

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.sstatic.net/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)
                                }

                            })
                        });

                    });



Upvotes: 0

Views: 350

Answers (1)

israel
israel

Reputation: 135

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

Upvotes: 2

Related Questions