![](/img/trans.png)
[英]How do I push my data (in key:value pair) into my Firebase realtime database in android?
[英]How do I update field in my item in realtime database?
我正在尝试更新我的实时数据库节点中的一个字段。 目前它正在工作,但我在单击按钮后出现错误,即使数据库中的值发生变化。 基本上改变favorite: true
或favorite:false
这些是我在按下按钮时收到的 2 个错误。
@firebase/database: FIREBASE 警告:用户回调抛出异常。 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' 未定义)
这是我当前用于更新值的代码。
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})
}
我通过以下解决了这个问题。 但是按钮的文本不会实时变化。
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.