Home > Blockchain >  How to add a new map field inside a parent map field in Firestore?
How to add a new map field inside a parent map field in Firestore?

Time:04-16

I want to add a new map field inside another map field. My data structure looks like this: enter image description here

I want to add a 'field b' after 'field a' inside 'Filed 1' (pardon my typo).

my code looks like this

 const collsRef = doc(db, "collections", collectionUid); // get the right document
 await setDoc(collsRef, { sellectData }, { merge: true });

but not only this is giving me an error, I believe this code would only add a new field at the same level as 'Filed 1'

enter image description here

the data type triggering the unsupported field value error is an JS object like so – do I need to parse it before comiting it to the db?

enter image description here

I've blanked out data and left the structure and syntax because it contained personal sensitive data. I hope it's enough to explain the problem.

many thanks in advance

CodePudding user response:

I'm not sure what the sellectData structure looks like but you have to construct an object that has the same structure from Firestore. See sample code below:

const collsRef = doc(db, "collections", collectionUid);

let sellectData = {
  "field b": { "key b" : "value b" }
}

await setDoc(collsRef, { 
  "Filed 1": sellectData
}, { merge: true });

This would result in (already corrected your typo): enter image description here


For more information, see Update fields in nested objects.

  • Related