【问题标题】:TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) at Function.all (<anonymous>) Firebase Cloud FunctionTypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) at Function.all (<anonymous>) Firebase Cloud Function
【发布时间】:2021-02-03 20:25:28
【问题描述】:

我是这样设置云功能的:

exports.changeIsVisibleFieldAfterDay = functions.pubsub
.schedule("every 2 minutes").onRun((context) => {
  const d = new Date();
  d.setDate(d.getDate() - 1);
  return db.collectionGroup("Moments")
      .where("isVisible", "==", true)
      .where("timestamp", "<=", d)
      .get()
      .then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
          Promise.all().doc.ref.update({isVisible: false}, {merge: true});
        });
        return null;
      });
});

当我插入 Promise.all() 以更新子集合中的所有文档时,我收到此错误:

TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
at Function.all (<anonymous>) 

当我不使用Promise.all() 时,文档会正确更新,但我知道如果我想更新大量文档,我需要Promise

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    根据 Mozilla JS reference promise.All 函数在输入时需要一组承诺,因为您没有提供一个错误警告您未定义(即无参数)不可迭代。

    其实你想要的就是下面的代码(已经测试过了):

    db.collectionGroup("Moments")
        .where("isVisible", "==", true)
        .where("timestamp", "<=", d)
        .get()
        .then((querySnap) => {
            // Create an array of the promises returned by update()
            let promises = [];
            querySnap.forEach((doc) => {
                promises.push(doc.ref.update({hello:"yes"}, {merge: true}))
            });
            // Resolve the promises
            Promise.all(promises)
                .then((x) => console.log("Docs updated"))
                .catch((err) => console.error(err));
        });
    

    【讨论】:

    • 推理是有道理的,它有效,谢谢!
    猜你喜欢
    • 2019-08-13
    • 2021-01-10
    • 2020-11-06
    • 2020-12-14
    • 2021-05-21
    • 1970-01-01
    • 2021-10-22
    • 2021-04-15
    • 1970-01-01
    相关资源
    最近更新 更多