简体   繁体   English

如何在实时数据库中更新项目中的字段?

[英]How do I update field in my item in realtime database?

I am trying to update one field in my Realtime database node.我正在尝试更新我的实时数据库节点中的一个字段。 Currently it is working but I am getting errors after clicking the button even the value in the database changes.目前它正在工作,但我在单击按钮后出现错误,即使数据库中的值发生变化。 Basically changing favorite: true or favorite:false基本上改变favorite: truefavorite:false

These are the 2 errors I am receiving when button is pressed.这些是我在按下按钮时收到的 2 个错误。

@firebase/database: FIREBASE WARNING: Exception was thrown by user callback. @firebase/database: FIREBASE 警告:用户回调抛出异常。 onValue@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.familyrecipebook&modulesOnly=false&runModule=true:149302:35 onValue@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.familyrecipebook&modulesOnly=false&runModule=true:149302:35

TypeError: this.snapshotCallback.call is not a function. (In 'this.snapshotCallback.call(null, expDataSnapshot, previousChildName)', 'this.snapshotCallback.call' is undefined) TypeError: this.snapshotCallback.call is not a function. (In 'this.snapshotCallback.call(null, expDataSnapshot, previousChildName)', 'this.snapshotCallback.call' 未定义)

This is my current code for updating the value.这是我当前用于更新值的代码。

const markOrRemoveFavorite = () => {
  const recipeRef = ref(db, 'users/' + userId + '/recepies' + `/${item.id}`)
  // const recipeData = {
  //   favorite: item.favorite ? false : true
  // }

  // const newRecipeKey = push(child(ref(db), 'users/' + userId + '/recepies' + `/${item.id}`)).key

  // const updates = {};
  // updates[newRecipeKey] = recipeData;
  return update(recipeRef, {favorite: item.favorite ? false : true})

}

I solved the issue by following.我通过以下解决了这个问题。 But text of the button doesnt change still in real time.但是按钮的文本不会实时变化。

    const recipeRef = ref(db, 'users/' + userId + '/recepies' + `/${item.id}`)
     const recipeData = {
       favorite: item.favorite ? false : true
     }

    update(recipeRef, recipeData).then(() => {
      if (item.favorite === false) {
        Toast.show({
          type: 'success',
          position: 'bottom',
          text1: 'Recipe',
          text2: `Recipe ${item.recipe} was added to favorites`,
        })
      } else {
        Toast.show({
          type: 'success',
          position: 'bottom',
          text1: 'Recipe',
          text2: `Recipe ${item.recipe} was removed from favorites`,
        })
      }
    })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的数据(在键:值对中)推送到 android 中的 Firebase 实时数据库中? - How do I push my data (in key:value pair) into my Firebase realtime database in android? 如何从 javascript 中的 firebase 实时数据库中获取子节点的总数? - How do I fetch the total number of child nodes from my firebase realtime database in javascript? 如何从 function 返回我的 Firebase 实时数据库的快照值? - How do I return the snapshot value of my Firebase Realtime Database from a function? 如何使我的 firebase 数据库的结果实时持续更新? - How to make my result from firebase database continuously update at realtime? 我如何消除实时数据库中自动生成的密钥 firebase flutter - How do i eliminate the autogenerated key in realtime database firebase flutter 如何更新实时数据库中的键值? - How can I update key values in realtime database? 如何将音频文件保存到 Firebase 实时数据库? - How do I save an audio file into Firebase Realtime Database? 如何从我的 Firebase 实时数据库的多个节点填充我的 RecyclerView? - How can I populate my RecyclerView from multiple nodes of my Firebase Realtime Database? 如何在 Firebase 实时数据库 Unity 中按字符串和数字中的值对数据库中的子项进行排序 - How do I order children in a database by value in strings and numbers in Firebase Realtime Database Unity 如何使用 useEffect 将 firebase 实时数据库中的更改显示到我的页面? - How can I show the change in firebase realtime database to my page with useEffect?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM