Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to update an "array of objects" with Firestore and Firebase functions?

I have some body data coming into firebase functions as a array ['123','hello','34']

I then process it by doing const data = JSON.stringify(['123','hello','34'])

After i update the firestore data by doing a set

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

set({testData : firestore.FieldValue.arrayUnion(data)})

Problem is, the entire thing is saved as string in firestore

testData: "['123','hello','34']"

Instead of an array.

testData: 
 0: "123"
 1: "hello"
 2: "34"

My question is how do i get firestore to save it as an array?

>Solution :

You are converting it to a string by using stringify(). Try refactoring the code as shown below:

docRef.set({ testData : firestore.FieldValue.arrayUnion(...data) })
// use spread operator here                             ^^^

The documentation (NodeJS) has an example on adding multiple values using arrayUnion.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading