我想为配方创建一个like
功能,以便当用户点击"喜欢"按钮它会在Firebase上生成类似的内容:
users:
Key:
like:
recipe
0: recipeKey,
1: recipeKey
about: aboutUser
email: userEmail
name: userName
imgURL: userImageURL
phone: userPhoneNumber
zipcode: userZipcode
我正在使用此代码来实现它:
this.db.object("users/" + key + "/like/").update({
recipe
});
其中"食谱"包含喜欢的" recipeKey"。它生成这样的东西:
users:
Key:
like:
recipe: recipeKey
about: aboutUser
email: userEmail
name: userName
imgURL: userImageURL
phone: userPhoneNumber
zipcode: userZipcode
问题是它没有附加新的recipeKey
。它总是用新的覆盖recipeKey
。
答案 0 :(得分:1)
您需要使用set
功能代替update
,例如;
function addNewRecipeKey(newRecipeKey, key){
var recipe = [];
recipeRef=firebase.database().ref("users/" + key + "/like/recipe");
recipeRef.once('value', snap=>{
if (snap.exists()) recipe = snap.val();
recipe.push(newRecipeKey); // newRecipe that you want to add into.
recipeRef.set(recipe);
})
}