【问题标题】:Make a Firebase Child ref with react Native使用 react Native 制作 Firebase Child ref
【发布时间】:2017-07-06 07:01:23
【问题描述】:

如何使用相对路径引用键?我想针对 item1/items2 等的“喜欢”子项……这些是我自己创建的关键。 这是我的 json 文件:

{
    "items": {
        "items1": {
            "french": "fdjsfd",
            "english": "feeds",
            "english_erudite": "fdsfds",
            "likes":"0",
            },
          "items2": {
            "french": "fdfsd",
            "english": "fdsfsd",
            "english_erudite": "feeds",
            "likes":"0",
            }
          }
}

现在我有这个,但它向节点“items”添加了一个新子节点,而不是转到 items>items1>likes:

var ref = firebase.database().ref();



var likes_words = ref.child("items").child("???").child("likes");


likes_words.transaction(function(currentLike) {
   return currentLike + 1;
});
}

【问题讨论】:

  • 您要增加哪个项目的计数?

标签: javascript reactjs firebase react-native firebase-realtime-database


【解决方案1】:

只需将您的代码调整为此

 var ref = firebase.database().ref();

            var likes_words = ref.child("items").once('value');

                likes_words.then(items => {
                    items.forEach(item => {

                       //for each item do this
                       let currentlike = item.val().likes;
                     ref.child("items/"+item.key+"/likes").update((currentlike+1));

                    })
               });

【讨论】:

  • 这将适用于单用户情况,但我建议using a transaction 以获得更强大的解决方案。 ref.child("items/"+item.key+"/likes").transaction(function(currentLikes) { return (currentLikes || 0) + 1; });
  • 它抛出一个语法错误为您的解决方案@FrankvanPuffelen 它指出这一行:ref.child("items/"+item.key+"/likes").transaction(函数(c‌​urrentLikes)
  • 据我所知,我在评论中留下的代码中没有>。因此,仅此更改似乎不太可能引入该错误。如果找不到原因,请编辑您的问题以包含 minimal code that reproduces the error
  • 谢谢@EmekaObianom,无法解决问题,但可以找到有效的代码;-)
  • 您也可以发布您的解决方案链接,以便对某人有所帮助。将其作为链接发布在评论上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 2021-01-03
  • 2017-12-23
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
相关资源
最近更新 更多