【发布时间】:2020-12-28 04:25:48
【问题描述】:
我每周运行一个脚本。当我的集合中的文档少于 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.js firebase google-cloud-firestore google-cloud-functions