【发布时间】:2022-01-15 20:58:02
【问题描述】:
我正在尝试编写一个 firebase 云函数,该函数在每次新用户创建帐户时运行一个简单的 while 循环。由于某种原因,更新功能只运行一次并停止。我使用的代码粘贴在下面
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();
const firestore = admin.firestore();
var data;
var counter = 0;
exports.onUserCreate = functions.firestore.document('testCollection/{docID}').onCreate(async(snapshot, context) =>{
data = snapshot.data();
while (counter < 5) {
setInterval(updateCounter(counter), 5000);
}
})
async function updateCounter(counter){
await firestore.collection('testCollection').doc(data['username']).update({
counter: admin.firestore.FieldValue.increment(1)
});
counter++;
}
【问题讨论】:
-
为什么在循环中使用 setInterval?
-
我希望“updateCounter”函数在设定的时间后运行 5 次
标签: node.js firebase google-cloud-firestore