0

How to use a variable such as users UID as the key to a map in Firestore.

I would imagine its the same principal as out of Firestore

const userID = "gfsueudiyeueuuuke7ue"

setDoc(docRef, {
    userID : {
        age: 11,
        name: "test"
        ... 
    }
})

Ive tried (userID): ... as well as ${userID}:

any suggestions?

aj griffin
  • 93
  • 6
  • 1
    Does this answer your question? [JavaScript set object key by variable](https://stackoverflow.com/questions/11508463/javascript-set-object-key-by-variable) – Zsolt Meszaros Dec 21 '21 at 23:30

1 Answers1

1

I was able to accomplish this by putting brackets [] around [userID], alos using updateDoc as to not overwrite the document.

const userID = "gfsueudiyeueuuuke7ue"

updateDoc(docRef, {
    [userID] : {
        age: 11,
        name: "test"
        ... 
    }
})
aj griffin
  • 93
  • 6