我想使用 Firestore 将变量用作字典中的键。
var uid = "123456";
await admin
.firestore()
.collection("shop")
.doc(shopId)
.update({
"role.${uid}": "reader",
});
但是上面的代码不起作用。
如何将
uid
设置为role
的键?
如果我理解您要正确执行的操作,您应该使用模板文字(反引号)而不是常规字符串(双引号)。
例如:
await admin
.firestore()
.collection("shop")
.doc(shopId)
.update({
`role.${uid}`: "reader",
});