【问题标题】:Firebase how to update child value in javasscriptFirebase如何在javascript中更新子值
【发布时间】:2020-05-04 07:05:12
【问题描述】:

我想根据 categoryId 更新子值。我尝试遵循本教程Firebase DB - How to update particular value of child in Firebase Database。这是工作,但它没有存储在同一个参考中。它存储在另一个参考文献中。

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)
                                }

                            })
                        });

                    });



【问题讨论】:

  • 更新前添加图片
  • @PeterHaddad 我添加了图片的链接

标签: javascript firebase vue.js firebase-realtime-database


【解决方案1】:

2 个问题: 1.您忘记在子路径上添加“\eventos”。 2.不要使用.set(),因为它会删除所有其他数据。 代替 .set() 使用 .update()。 试试这个代码:

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)
                                }

                            })
                        });

                    });

如果它仍然不起作用,请告诉我

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 2018-03-06
    • 2017-03-28
    相关资源
    最近更新 更多