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 Store A Element Inside A Map Using Dynamic Variable In Firebase

I Wanted To Know How We Can Store Elements(Map) Inside A Map With Dynamic Element Names.
I Tried The Following

var activequestionid = "12201"
var aqid = "answers."+activequestionid
await updateDoc((doc(db, "tests", testid), {
      [`${aqid}`]: {ans:ans,type:type,time:serverTimestamp()}
    }))
var activequestionid = "12201"
var aqid = "answers."+activequestionid
await updateDoc((doc(db, "tests", testid), {
      [aqid]: {ans:ans,type:type,time:serverTimestamp()}
    }))

Also My Map Is As Follows:

testid(document)
    answers(map)
        12201(map)

But Both Give The Same Error: Expected type 'Pc', but it was: a custom Object object
Any Help On This Issue Is Appreciated!

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

>Solution :

The updateDoc() function takes 2 parameters – a DocumentReference and the data. You have () around both of them. Try removing them:

var activequestionid = "12201"
var aqid = "answers."+activequestionid

await updateDoc(
  doc(db, "tests", testid), {
    [aqid]: {
      ans: ans,
      type: type,
      time: serverTimestamp()
    }
  })
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