我可以防止 Firebase set() 覆盖现有数据吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12684874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I prevent Firebase set() from overwriting existing data?
提问by Itumac
If I do this, all is good with my itemRef:
如果我这样做,我的 itemRef 一切都很好:
itemRef.child('appreciates').set(newFlag);
itemRef.child('id').set(newId);
other properties of itemRef remain BUT child_changed is called twice
itemRef 的其他属性保持不变但 child_changed 被调用两次
If I do this:
如果我这样做:
itemRef.set({appreciates:newFlag,id:newId});
child_changed is called only once but my other properties are destroyed. Is there a workaround besides the clumsy one of repopulating the entire reference object?
child_changed 只被调用一次,但我的其他属性被破坏。除了笨拙的重新填充整个参考对象之外,还有其他解决方法吗?
Thanks,
谢谢,
Tim
蒂姆
回答by Andrew Lee
The Firebase update() function will allow you to modify some children of an object while leaving others unchanged. The update function will only trigger one "value" event on other clients for the path being written no matter how many children are changed.
Firebase update() 函数将允许您修改对象的某些子对象,而保持其他子对象不变。无论更改了多少子级,更新函数都只会在其他客户端上为正在写入的路径触发一个“值”事件。
In this example, you could do:
在这个例子中,你可以这样做:
itemRef.update({appreciates:newFlag,id:newId});
Documentation for update() is here.
update() 的文档在这里。
回答by fionbio
You can create a rule that will prevent overwrites if data already exists. Reproduced here from Firebase docs Existing Data vs New Data
如果数据已经存在,您可以创建一个规则来防止覆盖。此处转载自Firebase 文档现有数据与新数据
// we can write as long as old data or new data does not exist
// in other words, if this is a delete or a create, but not an update
".write": "!data.exists() || !newData.exists()"
回答by RamiroIsBack
Now .updatetakes care of it, you can change existing data or add new one without affecting the rest of data you already had there.
现在.update负责它,您可以更改现有数据或添加新数据,而不会影响您已有的其余数据。
In this example, I use this function to set a product as sold, the product has other variables with data and may or may not have soldor sellingTimebut it doesn't matter cos if it doesn't exist will create them and if it does, will update the data
在这个例子中,我使用这个函数将一个产品设置为已售出,该产品有其他带有数据的变量,可能有也可能没有sold,sellingTime但没关系,因为如果它不存在就会创建它们,如果存在,将更新数据
var sellingProduct = function(id){
dataBase.ref('product/'+id).update({
sold:true,
sellingTime: Date.now(),
}).then (function(){
alert ('your product is flaged as sold')
}).catch(function(error){
alert ('problem while flaging to sold '+ error)
})
}
回答by Michael Stokes
I've been trying to do this having a structure like the following:
我一直在尝试使用如下结构来执行此操作:
The problem I was having was when running say set on specific fields such as name, description and date all of the other child nodes would then be removed with the following:
我遇到的问题是在特定字段(如名称、描述和日期)上运行 say set 时,所有其他子节点将被删除,如下所示:
return (dispatch) => {
firebase.database().ref(`/gigs/${uid}`)
.set({ name, description, date })
.then(() => {
dispatch({ type: GIG_SAVE_SUCCESS });
Actions.home({ type: 'reset' });
});
};
Leaving only the name, description and date nodes but using the following the specific nodes are updated without removing the other child nodes i.e. members, image etc:
只留下名称、描述和日期节点,但使用以下特定节点更新而不删除其他子节点,即成员、图像等:
return (dispatch) => {
var ref = firebase.database().ref(`/gigs/${uid}`);
ref.child('name').set(name)
ref.child('description').set(description)
ref.child('date').set(date)
.then(() => {
dispatch({ type: GIG_SAVE_SUCCESS });
Actions.home({ type: 'reset' });
});
};
回答by Dharmaraj
Though you can use update, you can also use this:
虽然您可以使用update,但您也可以使用它:
itemRef.set({appreciates:newFlag,id:newId}, {merge: true});


