有没有办法从 Firestore 中的数组更新特定索引 [英] Is there any way to update a specific index from the array in Firestore

查看:26
本文介绍了有没有办法从 Firestore 中的数组更新特定索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there any way to update a specific index from the array in 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})
    })
  }
}

推荐答案

有什么方法可以更新 Firestore 数组中的特定索引?

Is there any way to update a specific index from the array in Firestore?

不,没有!这是不可能的,因为如果您想执行更新,您需要知道该特定元素的索引.谈到 Cloud Firestore 阵列时,您可能会想到不同.

No, there is not! This is not possible because if you want to perform an update, you need to know the index of that particular element. When talking about Cloud Firestore arrays, the things are different that you might think.

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

Because we are creating apps that can be used in a multi user environment, try to think what might happen if a user wants to edit a value at index 0, some other user wants to delete the value at index 0 and in the same time some other user might want to add another item at index 0. For sure, you'll end up having very different results and why not, get even array out of bounds exception. So Firestore actions with arrays are a little bit different. So you cannot perform actions like, insert, update or delete at a specific index.

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

If those two methods do not help you enough, you should get the entire document, get the array, modify it and add it back to the database.

这篇关于有没有办法从 Firestore 中的数组更新特定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆