有没有办法从 Firestore 中的数组更新特定索引

Fhr*_*ldt 9 javascript firebase reactjs google-cloud-firestore

有没有办法从 Firebase/firestore 中的数组更新特定索引?

 export const editComment = (comment) => {
 return (dispatch, getState, { getFirebase, getFirestore }) => {
    const firestore = getFirestore();

    firestore.collection('topics').doc(comment.topicId).update({

     comments:  <--- this is array of objects


    }).then(() => {
      dispatch({ type: 'EDIT_COMMENT' })
    }).catch((error) => {
      dispatch({ type: 'EDIT_COMMENT_ERROR', error})
    })
  }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*amo 17

有没有办法从 Firestore 中的数组更新特定索引?

不,那里没有!这是不可能的,因为如果要执行更新,则需要知道该特定元素的索引。在谈论 Cloud Firestore 阵列时,您可能想到的情况有所不同。

因为我们正在创建可在多用户环境中使用的应用程序,请尝试考虑如果用户想要编辑索引 0 处的值,其他用户想要删除索引 0 处的值,同时某些用户想要删除该值会发生什么其他用户可能想在索引 0 处添加另一个项目。当然,你最终会得到非常不同的结果,为什么不,甚至数组越界异常。所以带数组的 Firestore 操作有点不同。因此,您无法在特定索引处执行诸如插入、更新或删除之类的操作。

如果这两种方法对您的帮助还不够,您应该获取整个文档,获取数组,修改它并将其添加回数据库。