【问题标题】:firebase iterate trough a collection and update all documentsfirebase 遍历集合并更新所有文档
【发布时间】:2021-04-29 15:48:42
【问题描述】:

我想遍历一个集合并更新文档。我找到了这篇文章:Is there a method to iterate through all documents in a collection in firestore 显示了如何读取数据。我重写了这段代码来更新文档。但它似乎不起作用,因为我需要写入数据:

const db = firebase.firestore();

db.collection("users_highscores").get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        doc.update({
            highscores_daily: {}
        })
     });
  });

我收到此错误:Uncaught (in promise) TypeError: doc.update is not a function

我认为迭代和编写我需要做一些根本不同的事情的数据,但我现在不知道。我希望你能帮助我

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    我只是想读取 id 然后写入这个 id。这可能不是最有效的方法,但它确实有效:

       db.collection("users_highscores").get().then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
                db.collection("users_highscores").doc(doc.id).update({
                    highscore_daily: {}
                })
            });
        });
    

    但我希望看到更有效且不会浪费任何读取的东西。

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 2020-02-18
      • 2018-12-31
      相关资源
      最近更新 更多