[英]Overwrite data without generating new key/node
I'm trying to overwrite some data in my database.我试图覆盖我的数据库中的一些数据。 The structure is simple, being:
结构很简单,就是:
recipes {
user_1{
recipe_1{data}
recipe_2{data}
}
user_2{
recipe_1{data}
recipe_2{data}
}
}
I'm trying to update an item and overwrite it with any changes using this, where recipe
is the new object with the updated data I'm trying to save, and key
is the unique key generated for each item in the database.我正在尝试更新一个项目并使用此
recipe
使用任何更改覆盖它,其中recipe
是具有我尝试保存的更新数据的新对象, key
是为数据库中的每个项目生成的唯一键。 In the database itself the labels recipe_1
and so on would be replaced with their unique keys.在数据库本身中,标签
recipe_1
等将被替换为它们的唯一键。 So this path should be just replacing the entire recipe_2
instead of adding a new node.所以这个路径应该只是替换整个
recipe_2
而不是添加一个新节点。
firebase.database().ref('recipes/' + userID + "/" + key).set({ recipe });
So say I was attempting to update user_1's
recipe_2
with this, after going through, the database structure would then be:所以说我试图用这个更新
user_1's
recipe_2
,经过之后,数据库结构将是:
data {
user_1{
recipe_1{data}
recipe{
recipe_2{data}
}
}
user_2{
recipe_1{data}
recipe_2{data}
}
}
How can I overwrite the item and keep the structure of the database the same while changing the contents of the recipe?如何在更改配方内容的同时覆盖项目并保持数据库结构不变?
You need to remove the extra {}
from your set
function which is creating a new node in the key value you are trying to update.您需要从
set
函数中删除额外的{}
,该函数会在您尝试更新的键值中创建一个新节点。
Change your code to将您的代码更改为
firebase.database().ref('recipes/' + userID + "/" + key).set(recipe);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.