首页 > 解决方案 > Firebase 函数执行耗时 60004 毫秒,完成状态为:“超时”错误:超出内存限制。函数调用被中断

问题描述

我每周运行一个脚本。当我的集合中的文档少于 9k 时,一切正常。我已经尝试使用异步函数等到 foreach 结束。但它没有用。你能帮我处理异步功能吗?

代码:

exports.weeklyPointReset = functions.pubsub.schedule('12 04 * * 1').timeZone('UTC').onRun((context) => {


  return db.collection('Users').where('weeklyPoint', '!=', 0).orderBy('weeklyPoint', 'desc').get().then(async (snapshot) => {
    var counter = 1;
    if (snapshot.empty) {
      return null;
    } else {
      const promises = [];
      await snapshot.forEach(async (doc) => {
        await promises.push(doc.ref.update({ weeklyPoint: 0 }));
      });
      return await Promise.all(promises);
    }
  }).catch(error => {
    console.log(error);
    return null;
  });
});

标签: node.jsfirebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


感谢您的回复。Renaud,我找到了像你提到的那样增加记忆力的方法。解决方案;

  1. 转到https://console.cloud.google.com/functions/list
  2. 选择函数然后点击编辑,
  3. 设置内存大小和超时值。(超时最大限制为 540sn)

推荐阅读