【发布时间】:2019-12-12 09:01:22
【问题描述】:
我正在尝试按固定时间表生成报告。但是我遇到了一个问题,在该函数将运行的当前时间之前,我无法检索从今天开始的日期。
exports.generateReport = functions.pubsub.schedule('36 15 * * *').onRun(async (context) => {
console.log(context);
const currentTime = admin.firestore.Timestamp.now();
const shopSnapshot = await db.collection("shops").get();
let shopDoc = shopSnapshot.docs.map(doc => doc.data());
const promises = [];
let transactionList = [];
let reportList = [];
let i = 0;
console.log(shopDoc);
shopDoc = shopDoc.filter(shop => !!shop.key);
console.log(shopDoc);
for(var j=0; j<shopDoc.length; j++){
console.log("Enter shop ID:"+shopDoc[j].key);
promises.push(db.collection("shops").doc(shopDoc[j].key).collection("transactions").get());
}
const snapshotArrays = await Promise.all(promises);
snapshotArrays.forEach(snapArray => {
snapArray.forEach(snap => {
//console.log(snap.data());
transactionList.push({data: snap.data(), key: shopDoc[i].key});
})
i++;
});
for(var k=0; k<shopDoc.length; k++){
let amount = 0;
for (var l=0; l<transactionList.length; l++){
if(shopDoc[k].key === transactionList[l].key){
console.log("get date");
if (transactionList[l].data.createAt < currentTime){
amount += transactionList[l].data.amount;
console.log(amount);
}
}
}
reportList.push({amount: amount, key: shopDoc[k].key});
}
console.log(reportList);
console.log(transactionList);
});
我尝试使用new Date() 也与一串与 Firestore 时间戳格式完全相同的日期字符串进行比较,但所有交易仍然出现在该时间之前或此时不包含任何交易。
【问题讨论】:
-
出于测试目的,这就是我输入
functions.pubsub.schedule('36 15 * * *')的原因。我想检查所有交易文件 createAt 是否是今天的日期。那我只取今天日期的所有交易文件 -
好的,谢谢你的回答(我同时删除了我的评论......因为我看到你在做
transactionList[l].data.createAt < currentTime) -
如果我正确理解您的业务需求,您想以某种方式在明天 00:01 运行 Cloud Function 并选择今天的所有交易,这样您就可以生成包含所有交易的报告前一天。对吗?
-
是的,我想获取从今天到当前时间的日期
-
是的,没错
标签: node.js firebase google-cloud-firestore google-cloud-functions