下面的代码扫描所有想要存档的用户:函数archiveColab()
问题是该函数位于一个 forEach 中,该扫描/users/节点,然后需要扫描/collaborative子节点,因此它在中有另一个forEach。
当我运行arquivaColab函数时,它会覆盖月中的每日记录,而不计算每个周期的总数,只留下那个月的最后一个周期。
这是由于异步执行forEach,我无法找到一种方法来顺序地读取和写入这些记录。
当添加等待/混杂时,显然循环在执行第一个周期/记录之后结束。
我理解这是一个理解异步模式如何工作的问题,我已经尝试了几天来一直试图找到一个解决方案。
任何帮助都是非常欢迎的。
代码:
exports.todoDia = functions.runWith({memory: '1GB', timeoutSeconds: '300'}).pubsub.schedule('10 15 * * *').timeZone('America/Sao_Paulo').onRun( async () => {
const cutoff = format(new Date().setDate(new Date().getDate() - 7), 'Y-m-d'); // 7 days ago
var userRef = admin.database().ref("users");
return userRef.once('value').then((snapshot) => {
snapshot.forEach(user => {
var userID = user.key;
if (userID === "F54jCdsPt8ZLIF17dYn8U0knMMY2") { // Only this user for testing
console.log(userID + " " + cutoff);
user.child('collaborative').forEach(async collaborative => {
if (collaborative.key <= cutoff) { await arquivaColab(userID, collaborative.key, collaborative.val()); }
});
}
});
return null;
});
});
async function arquivaColab(userID, keyColab, valColab) {
console.log("userID: "+userID+" | keyColab: "+keyColab+" | valColab: "+valColab);
const ref = admin.database().ref("/collaborative/"+userID+"/"+keyColab.substring(0,7));
var stringDest = "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0";
return ref.once('value').then((snapDest) => {
if (snapDest.exists()) { stringDest = snapDest.val() }
var wordsOrig = valColab.split(" ");
var wordsDest = stringDest.split(" ");
var strFinal = "";
for (var i = 0; i < wordsOrig.length; i += 1) {
strFinal += (Number(wordsOrig[i]) + Number(wordsDest[i])) + " ";
}
console.log("DESTINO: /collaborative/"+userID+"/"+keyColab.substring(0,7)+" : "+strFinal);
return admin.database().ref("/collaborative/"+userID+"/"+keyColab.substring(0,7)).set(strFinal.trim());
// console.log("ORIGEM : /users/"+userID+"/collaborative/"+keyColab+" : "+valColab);
// admin.database().ref("/users/"+userID+"/collaborative/"+keyColab.set(null);
});
}杰森:
"users" : {
"F54jCdsPt8ZLIF17dYn8U0knMMY2" : {
"collaborative" : {
"2021-03-12" : "0 0 0 0 0 0 1 0 0 2 0 0 0 0 0 0 0 0 0 0",
"2021-03-15" : "0 0 0 0 0 0 1 0 0 2 0 0 0 0 0 0 0 0 0 0",
"2021-07-07" : "0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 0 0 0"
},
"username" : "John Doe"
},
"Z5EEkzyG2WPlCHa9a6gDpBnIr2u2" : {
"collaborative" : {
"2021-07-06" : "0 0 0 0 0 0 1 0 0 2 0 0 0 0 0 0 0 0 0 0",
"2021-07-07" : "0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 0 0 0"
},
"username" : "John Next"
},
},要理解所需的功能:每个用户都有一个名为“协作”的节点。
YYYY MM-DD:"0 0 0
每天有20个计数器记录每个用户的不同活动。7天后,Firebase函数必须对这些记录进行归档,在/ counter /中计算每月记录中每个计数器的总和。
发布于 2021-07-12 21:46:16
forEach不支持async/await。您需要将数据存储到数组中,并使用支持async/await的async/await循环,如下所示:
return userRef.once("value").then(async (snapshot) => {
const tempArray = [];
if (userID === "F54jCdsPt8ZLIF17dYn8U0knMMY2") {
// Only this user for testing
console.log(userID + " " + cutoff);
user.child("collaborative").forEach(async (collaborative) => {
if (collaborative.key <= cutoff) {
tempArray.push({
userID,
key: collaborative.key,
val: collaborative.val(),
});
}
});
}
for (let i = 0; i < tempArray.length; i++) {
const { userID, key, val } = tempArray[i];
await arquivaColab(userID, key, val);
}
});
return null;https://stackoverflow.com/questions/68353012
复制相似问题