Firestore更新数组中的特定元素 [英] Firestore update specific element in array

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

问题描述

我正在将Google Firestore与Angular结合使用,并且正在尝试更新数组中的特定元素.

Im using Google Firestore with Angular and I'm trying to update a specific element inside an array.

我尝试了很多事情,但一点都不成功.

I've tried a lot of things but with no succsess at all.

我如何才能到达公司数组中的特定公司索引?

How can I reach to the specific company index inside the companies array?

谢谢:)

推荐答案

我如何才能到达公司数组中的特定公司索引?

How can I reach to the specific company index inside the companies array?

您无法根据相应的索引获取,更新或删除Firestore数组中的元素.试想一下,如果一个用户想要编辑索引0处的值,而另一个用户想要删除索引0处的值,而另一个用户想要在索引1处添加另一个值,那将会发生什么? ArrayIndexOutOfBoundsException .因此,带有数组的Firestore操作略有不同.因此,您无法执行特定操作,例如在特定索引处插入,更新或删除.因此,仅在不关心存储元素的确切顺序时才使用数组.

There is no way you can get, update or remove an element in a Firestore array according with the corresponding index. 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 another user wants to add another value at index 1, you'll end up having very different results and why not, an ArrayIndexOutOfBoundsException. So Firestore actions with arrays are a little bit different. So you cannot perform actions like, insert, update or delete at a specific index. So use arrays only if don't care about the exact order that you store elements.

Firestore于几周前添加了一些功能,以添加或删除特定元素,但前提是不关心它们的确切位置.请参阅此处官方文档.

Firestore added a few weeks ago some features to add or remove specific elements but only if don't care about the exact position of them. See here official documentation.

在您的情况下,您没有一个简单的字符串数组,而有一个对象数组,这意味着为了进行一些改动,您应该获取整个对象,遍历属性,进行必要的更改并将其写回数据库.

In your case, you don't have a simple array of strings you have an array of objects, which means that in order to do some chnages, you should get the entire object, iterate over the properties, make the necessary changes and write it back to the database.

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

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