如果我这样做,我的itemRef一切都很好:
itemRef.child('appreciates').set(newFlag);
itemRef.child('id').set(newId);
itemRef的其他属性仍为BUT,child_changed被调用两次
如果我这样做:
itemRef.set({appreciates:newFlag,id:newId});
child_changed仅被调用一次但我的其他属性被销毁。 除了重新填充整个参考对象的笨拙之外,还有一种解决方法吗?
谢谢,
添
答案 0 :(得分:17)
Firebase update()函数允许您修改对象的某些子项,同时保持其他子项不变。更新功能只会触发一个"值"无论有多少孩子被改变,其他客户的事件都会被写入。
在此示例中,您可以执行以下操作:
itemRef.update({appreciates:newFlag,id:newId});
update() is here的文档。
答案 1 :(得分:2)
如果数据已存在,您可以创建一个可以防止覆盖的规则。 从Firebase docs Existing Data vs New Data
转载// 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()"
答案 2 :(得分:1)
我一直在尝试这样做,结构如下:
我遇到的问题是,在名称,描述和日期等特定字段上运行时,将删除所有其他子节点,并使用以下内容删除:
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' });
});
};
答案 3 :(得分:1)
现在 .update 处理它,您可以更改现有数据或添加新数据,而不会影响您已经拥有的其他数据。
在此示例中,我使用此功能将产品设置为已售出,产品包含其他带数据的变量,可能有也可能没有th
或sold
,但这并不重要cos如果它不存在将创建它们,如果存在,将更新数据
sellingTime
答案 4 :(得分:0)
尽管您可以使用update
,但您也可以使用以下方法:
itemRef.set({appreciates:newFlag,id:newId}, {merge: true});