1 node.js firebase google-cloud-functions google-cloud-firestore
我正在尝试按固定时间表生成报告。但是我遇到了一个问题,在该函数运行的当前时间之前,我无法检索今天的日期。
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);
});
Run Code Online (Sandbox Code Playgroud)
我new Date()也尝试使用与 Firestore Timestamp 格式完全相同的日期字符串进行比较,但仍然所有事务都出现在该时间之前或此时不包含任何事务。
例如,如果我正确理解您希望通过在今天 00:05 运行计划云函数来为昨天发生的所有事务生成每日报告,那么这里是使用moment.js库的一种可能方法:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const moment = require('moment');
admin.initializeApp();
exports.generateReport = functions.pubsub.schedule('05 00 * * *').onRun(async (context) => {
let m1 = moment();
let m2 = moment();
m1.add(-1, 'days');
m2.add(-1, 'days');
m1.startOf('day');
m2.endOf('day');
const shopSnapshot = await db.collection("shops").get();
let shopDoc = shopSnapshot.docs.map(doc => doc.data());
const promises = [];
let transactionList = [];
let reportList = [];
let i = 0;
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")
.orderBy("createAt")
.where("createAt", ">", m1.toDate())
.where("createAt", "<=", m2.toDate())
.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){
amount += transactionList[l].data.amount;
}
}
reportList.push({amount: amount, key: shopDoc[k].key});
}
//.....
});
Run Code Online (Sandbox Code Playgroud)
那么,我们在这段代码中做了什么?首先,我们创建两个时刻对象,并将它们的日期设置为昨天。然后,startOf()和endOf()我们调整第一个昨天的时间为12:00:00.000 AM(即00:00:00,见这里),第二个到昨天11:59:59.999 PM(即23:59 :59)。
有了这两个日期,对于每个交易集合,我们按如下方式调整查询,调用toDate()方法:
db.collection("shops")
.doc(shopDoc[j].key)
.collection("transactions")
.orderBy("createAt")
.where("createAt", ">", m1.toDate())
.where("createAt", "<=", m2.toDate())
.get();
Run Code Online (Sandbox Code Playgroud)
就是这样。这里的一大优势是过滤是在 Firestore 数据库(在后端)中完成的,而不是像您在问题中所做的那样在前端完成 ( if (transactionList[l].data.createAt < currentTime){...})
| 归档时间: |
|
| 查看次数: |
1677 次 |
| 最近记录: |