如何使用 JavaScript 在字典中设置键值?

问题描述 投票:0回答:1

我想使用 Firestore 将变量用作字典中的键。

var uid = "123456";

await admin
          .firestore()
          .collection("shop")
          .doc(shopId)
          .update({
            "role.${uid}": "reader",
          });

但是上面的代码不起作用。

结果来了。 enter image description here

如何将

uid
设置为
role
的键?

javascript typescript firebase google-cloud-firestore
1个回答
0
投票

如果我理解您要正确执行的操作,您应该使用模板文字(反引号)而不是常规字符串(双引号)。

例如:

await admin
          .firestore()
          .collection("shop")
          .doc(shopId)
          .update({
            `role.${uid}`: "reader",
          });
© www.soinside.com 2019 - 2024. All rights reserved.