![](/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.