0

I have an array within a map that I want to update using Firestore Functions. My data structure looks like this:

enter image description here

In this case, I want to update the shareRecordsWith array within the {uid} map.

I have been trying to use the following firestore functions but it's not working.

const recordsDict = {"uid": aboutUID, "accessType": accessType}
profilesRef.doc(uid).set({
    [uid]: {
        {'shareRecordsWith': admin.firestore.FieldValue.arrayUnion(recordsDict)},
        {merge: true}
    }
});

Is there a way to update an array within a map through Firestore Functions?

EDIT: Based on the suggestions, I changed the function to

profilesRef.doc(uid).set({
    [`${uid}.shareRecordsWith`]: admin.firestore.FieldValue.arrayUnion(recordsDict)
}, { merge: true })

Unfortunately, then it adds ${uid}.shareRecordsWith as a new field in the document instead of adding to the shareRecordsWith array within the {uid} map.

This is what it looks like:

enter image description here

0

1 Answer 1

1

You will need to call out the entire path of the nested field as a single property. You can do this by passing a FieldPath object with update(), but it doesn't look like set() supports this.

profilesRef.doc(uid).update(
    new admin.firestore.FieldPath(uid, "shareRecordsWith"),
    admin.firestore.FieldValue.arrayUnion(recordsDict)
})
Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, it's adding ${uid}.shareRecordsWith as a new field in the document instead of adding to the shareRecordsWith array within the {uid} map. I've edited and attached a screenshot in my original post.
Looks like you will have to use update() with FieldPath.
Worked your solution after I took out the close bracket! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.