【发布时间】:2019-05-28 16:17:36
【问题描述】:
我需要在 Firebase 数组中的特定索引处更新对象中的值字段
我试图通过所有对象抓取getState() 数组;
然后获取我需要的对象索引;
然后为对象分配新值,在本例中为内容;
然后将整个数组 (comments) 重写为 newArray (actualComments),如下所示。
这是我想要的,但只是第一次。如果我再次尝试这样做,我会收到错误 TypeError: "content" is read-only。
export const editComment = (comment) => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
let actualComments = getState().firestore.data.topics[comment.topicId].comments;
let numberArray = actualComments.findIndex(e => {return e.id === comment.commentId});
actualComments[numberArray].content = comment.editContent;
firestore.collection('topics').doc(comment.topicId).update({
comments: actualComments
}).then(() => {
dispatch({ type: 'EDIT_COMMENT' })
}).catch((error) => {
dispatch({ type: 'EDIT_COMMENT_ERROR', error})
})
}
}
【问题讨论】:
-
如果你使用 redux 作为你的状态管理器,你的状态应该是不可变的,我觉得它第一次没有抱怨很奇怪。您是否尝试过通过 reducer 更新数据?
-
我尝试在 reducer 之前以异步操作(thunk)更新 firestorm 数据库中一个对象的值。我从数据库动态呈现给 DOM 的每个“评论”数据都在数组中的对象内部,
标签: javascript reactjs firebase redux google-cloud-firestore