【发布时间】:2018-01-22 08:59:14
【问题描述】:
我有一个这样的 Firebase 数据库结构:
-KrVtLbX7w8LKHC888cx
siteInfo
description: "Udemy is an online learning and teaching market..."
siteName: "Udemy"
title: "Udemy Online Courses - Learn Anything, On Your ..."
type: "video_lecture"
url: "https://www.udemy.com/"
每个siteInfo 节点都可能包含不同的键(即,一个可能没有type,另一个可能有img)。这使得数据键相对动态,因此我无法在firebase.update 中明确设置它们。
在前端,我有一个表单供用户编辑站点信息。该表单在各个输入字段中显示数据,供用户根据用户选择的站点进行编辑,这非常有效。
我的问题是向用户更新的数据节点提交更新。
现在我正在为具有如下变化的输入设置状态:
handleChange = ({target}) => {
this.setState({
[target.name]: target.value
});
}
并像这样更新节点:
firebase.database().ref().child(`paths/${this.props.uid}/${key}`);
let siteInfo = this.state;
firebaseRef.update({
siteInfo
});
这样做是删除整个siteInfo 节点并仅添加用户已编辑的那些字段。因此,不要在仅更新描述的情况下获取此信息:
-KrVtLbX7w8LKHC888cx
siteInfo
description: "The updated description!"
siteName: "Udemy"
title: "Udemy Online Courses - Learn Anything, On Your ..."
type: "video_lecture"
url: "https://www.udemy.com/"
我正在更新用户所做的编辑,但其他所有内容都被删除:
-KrVtLbX7w8LKHC888cx
siteInfo
description: "The updated description!"
siteName: "The updated site name!"
我希望它只更新那些已更新的密钥。这是我对 Firebase 的 .update 方法应该如何工作的理解,但由于某种原因,它只是覆盖了这里的所有内容。
【问题讨论】:
标签: javascript reactjs firebase firebase-realtime-database