我可以防止Firebase set()覆盖现有数据吗?

我可以防止Firebase set()覆盖现有数据吗?,firebase,Firebase,如果我这样做,我的itemRef就一切都好: itemRef.child('appreciates').set(newFlag); itemRef.child('id').set(newId); itemRef的其他属性保持不变,但子_被调用两次 如果我这样做: itemRef.set({appreciates:newFlag,id:newId}); child_changed只调用一次,但我的其他属性将被销毁。 除了笨拙地重新填充整个参照对象之外,还有其他解决方法吗 谢谢 TimFireb

如果我这样做,我的itemRef就一切都好:

itemRef.child('appreciates').set(newFlag);
itemRef.child('id').set(newId);
itemRef的其他属性保持不变,但子_被调用两次

如果我这样做:

itemRef.set({appreciates:newFlag,id:newId});
child_changed只调用一次,但我的其他属性将被销毁。 除了笨拙地重新填充整个参照对象之外,还有其他解决方法吗

谢谢

Tim

Firebase update()函数将允许您修改对象的某些子对象,而保留其他子对象不变。对于正在写入的路径,无论更改了多少子级,update函数都只会在其他客户端上触发一个“value”事件

在本例中,您可以执行以下操作:

itemRef.update({appreciates:newFlag,id:newId});

.

的文档我一直在尝试这样做,其结构如下:

return (dispatch) => {
    firebase.database().ref(`/gigs/${uid}`)
        .set({ name, description, date })
        .then(() => {
            dispatch({ type: GIG_SAVE_SUCCESS });
            Actions.home({ type: 'reset' });
        });
};

我遇到的问题是,在特定字段(如名称、说明和日期)上运行say set时,所有其他子节点都将被删除,如下所示:

return (dispatch) => {
    firebase.database().ref(`/gigs/${uid}`)
        .set({ name, description, date })
        .then(() => {
            dispatch({ type: GIG_SAVE_SUCCESS });
            Actions.home({ type: 'reset' });
        });
};
仅保留名称、说明和日期节点,但使用以下方法更新特定节点,而不删除其他子节点,即成员、图像等:

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' });
        });
}; 

如果数据已经存在,您可以创建一个规则来防止覆盖。 复制自


现在,.update处理好了,您可以更改现有数据或添加新数据,而不会影响已有的其他数据

在本例中,我使用此函数将产品设置为已售出,该产品具有其他带有数据的变量,并且可能有或可能没有
salled
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)
 })

}

虽然您可以使用
更新
,但您也可以使用:

itemRef.set({appreciates:newFlag,id:newId}, {merge: true});

我们有一些未记录的新特性来解决这个问题。你能给我发邮件到firebase.com的andrew吗?我想这已经包含在“更新”中了。如果你接受了回复的好人的回复,那就太好了@iTunes是否可以只写入新数据,而不覆盖现有数据?请参阅下面firebase规则的答案。是的!我也有过类似的经历,我做了
.child(…).set(…)
解决了这个问题。很想知道原因。@Itumac这就是你一直在寻找的答案