Reputation: 2515
Function executes when a write is made in collection logs
, it checks if doc exists in collection totals
. If doc exists it is trying to update number
object with number+1
at [0] of array.
Here is my code:
...//some code
var washingtonRef = admin.firestore().collection('totals').doc(entryDate_show);
washingtonRef.get().then((doc: any)=> {
if (doc.exists) {
console.log("doc found");
console.log("Document data:", doc.data());
washingtonRef.update({
[auth_ID]: admin.firestore.FieldValue.arrayUnion(
{ number: doc.data().number+1, // here it is trying to do +1
fullname: fullname,
authid: auth_ID },
)
});
...//some code
Problem: It is not working as expected
In array, it is adding new object [1]
with number : NaN
Expected behaviour: number : 2 at [0]
Attaching pic of the console:
Upvotes: 0
Views: 410
Reputation: 317362
FieldValue.arrayUnion()
adds a new element to an array field. That's why you're seeing a new object.
Firestore provides no update operation that uses the index of an array item to modify it. If you want to update an item in an array by index, you have to read the document, modify the array in memory, then update the array field back to the document.
Upvotes: 1