【问题标题】:Using String as JSON key for Firebase update() [duplicate]使用字符串作为 Firebase 更新()的 JSON 键 [重复]
【发布时间】:2021-11-19 21:20:31
【问题描述】:

以下 JavaScript 更新了我的 firebase:

db.collection("users").doc(user.uid).update({
                         img1: pic
                      });

我的数据库中有 img1、img2、img3。我想使用 for 循环来更新值,如下所示:

for (let i = 1; i <= 3; i++) {
  let s = "img" + i;

  db.collection("users").doc(user.uid).update({
                         s: pic
                      });
}

不幸的是,这不起作用。还有其他方法可以实现吗?

【问题讨论】:

    标签: javascript json firebase google-cloud-firestore


    【解决方案1】:

    在您的循环中,像这样更改代码:

    db.collection("users").doc(user.uid).update({
        [s]: pic
    });
    

    这样,您将使用 s 的值作为要更新的字段的名称。

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 1970-01-01
      • 2013-11-17
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2014-06-22
      • 2014-06-13
      相关资源
      最近更新 更多