【问题标题】:Use variable as map key in firestore在firestore中使用变量作为映射键
【发布时间】:2022-01-22 13:31:56
【问题描述】:

如何使用用户 UID 等变量作为 Firestore 中地图的键。

我会想象它与 Firestore 之外的主体相同

const userID = "gfsueudiyeueuuuke7ue"

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

我尝试过(用户 ID):... 还有${userID}:

有什么建议吗?

【问题讨论】:

标签: javascript firebase dictionary google-cloud-firestore javascript-objects


【解决方案1】:

我可以通过在 [userID] 周围加上方括号 [] 来实现这一点,并且使用 updateDoc 来不覆盖文档。

const userID = "gfsueudiyeueuuuke7ue"

updateDoc(docRef, {
    [userID] : {
        age: 11,
        name: "test"
        ... 
    }
})

【讨论】:

    猜你喜欢
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2018-10-02
    相关资源
    最近更新 更多