我需要在 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})
})
}
}