[英]How to change the value of a certain index of an array field of a document in Firestore?
I have an angular app with cloud firestore with the following structure:我有一个带有云 firestore 的角度应用程序,其结构如下:
--nombre
--grupo
--nombre1
--nombre2
--nombre3
--numerocuenta1
--numerocuenta2
--numerocuenta3
--resuelto
--0: false
--1: false
.
.
.
--13: false
As you can see, at the end I have an array of boolean (resuelto) which contains 13 elements, I want to change a value of a certain index when an element with the same index is clicked如您所见,最后我有一个包含 13 个元素的布尔值 (resuelto) 数组,我想在单击具有相同索引的元素时更改某个索引的值
¿Is there a way to access each index of that array and change its value? ¿有没有办法访问该数组的每个索引并更改其值?
I tried with this:我试过这个:
this.taskDoc = this.afs.doc(`tasks/${task`);
this.taskDoc.update(resuelto[0]); <--
And this:还有这个:
this.taskDoc.update(resuelto.0); <--
Firestore doesn't support update of individual array elements. Firestore 不支持更新单个数组元素。 If you have an array and want to change one of its value, you'll have to read the entire array, change the value locally, then write the entire array back.
如果您有一个数组并想更改其中一个值,则必须读取整个数组,在本地更改值,然后将整个数组写回。
It might be better for you to model your data as an object rather than an array.将数据建模为对象而不是数组可能更好。 Object properties may be individually updated by calling out the object field with the dot notation .
可以通过使用点符号调出对象字段来单独更新对象属性。
While you cannot do operations on certain elements in an array by index, you can add or remove elements in an array by value using arrayUnion and arrayRemove:虽然您不能按索引对数组中的某些元素进行操作,但您可以使用 arrayUnion 和 arrayRemove 按值添加或删除数组中的元素:
doc.update("someField" : FieldValue.arrayUnion("someValue"))
doc.update("someField" : FieldValue.arrayRemove("someValue"))
This makes arrays act more like a set then an array but it at least gives you some options.这使得数组更像是一个集合而不是一个数组,但它至少给了你一些选择。 more documentation here: https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html
更多文档在这里: https ://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.