aj griffin
aj griffin

Reputation: 101

Use variable as map key in firestore

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?

Upvotes: 0

Views: 593

Answers (1)

aj griffin
aj griffin

Reputation: 101

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"
        ... 
    }
})

Upvotes: 1

Related Questions