【发布时间】:2019-05-10 18:09:13
【问题描述】:
我正在尝试在 React 应用程序中写入 Firebase DB。如果我使用数据库引用和设置方法,它会添加新的数据条目。但是,当我通过 redux 操作使用相同的方法时,它会写入一个全新的数据。
这是有效的代码。
const notesRef = firebase.database().ref('notes');
const noteRef = notesRef.child(this.state.noteId);
const note = {
text: this.state.text,
timestamp: this.state.timestamp,
title: this.titleTextArea.value,
}
noteRef.set(note);
这是动作
export const addNote = (newNoteId, newNote) => async dispatch => {
notesRef.child(newNoteId).set(newNote);
};
const note = {
text: this.state.text,
timestamp: this.state.timestamp,
title: this.titleTextArea.value,
}
this.props.addNote(this.state.noteId, note);
不好的结果:
notes: "a6ff48e4-f34a-483f-a639-b0dbfd703009"
好结果:
notes:
b85def67-3877-4d2f-b5c7-23d8295768ad
text: "{\"ops\":[{\"insert\":\"fsasfs\\n\"}]}"
timestamp: 1557467143056
title: "asfa"
e4290153-a40c-464f-a92a-39eded74bd2a
text: "{\"ops\":[{\"insert\":\"sadfasdfsd\\n\"}]}"
timestamp: 1557467154054
title: "asdfsdaf"
【问题讨论】:
-
你在哪里调度动作?
-
@RahulRana 你在哪里是什么意思?
-
@RahulRana 啊...我明白你的意思了。我有一个错误的操作文件,我以为我删除了,这个文件被导入到我的组件中。现在可以了。谢谢!
-
@RahulRana 不知何故,一个已删除的文件在退出应用程序之前仍然存在,所以直到 Ctrl-C 并重新运行应用程序时我才收到错误。
-
@RahulRana 删除的文件包含 notesRef.set(newNote) 这就是它设置整个数据库的原因。
标签: reactjs firebase firebase-realtime-database react-redux