简体   繁体   中英

Modify arrays in Firebase Firestore

The Firestore database has the following field structure

images: {day: ['url1', 'url2', 'url3,] night: ['url1']}

First I would like to be able to append a value to the arrays. Did try the following:

await updateDoc(doc(firestore, FIRSTORE_COLLECTIONS.IMAGES, id), {
        [folder]: {
          [key]: arrayUnion(fileUrl)
        }

It went to the right images: path but unfortunately this just created a new array with one value and removed the ones that where there.

Secondly. If I also like to be able to create a new array in the same path with a key that does not exist can this be done and how could that be solved with this setup any ideas?

dependencies: "firebase": "^9.1.1", "react": "^17.0.2"

This is what solved it:

 await updateDoc(ref, {
        [`${folder}.${key}`]: arrayUnion(fileUrl)
      });

Proper path where the arrayUnion methods is implemented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM