gpt4 book ai didi

node.js - 如何查询 Firestore 今天所有文档的日期?

转载 作者:行者123 更新时间:2023-12-05 00:57:55 25 4
gpt4 key购买 nike

我正在尝试按固定时间表生成报告。但是我遇到了一个问题,在该函数将运行的当前时间之前,我无法检索从今天开始的日期。

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);

});

This is Firestore document and collection images

我尝试使用 new Date()还与一串与 Firestore Timestamp 格式完全相同但仍出现在该时间之前的所有事务或此时不包含事务的日期字符串进行比较。

最佳答案

如果我正确理解您希望通过在今天 00:05 运行计划 Cloud Function 来为昨天发生的所有交易生成每日报告,那么这里是使用 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});
}

//.....

});

那么,我们在这段代码中做了什么?首先,我们创建两个时刻对象,并将它们的日期设置为昨天。然后,使用 startOf() endOf() 我们将第一个时间调整为昨天上午 12:00:00.000(即 00:00:00,参见 here),第二个调整为昨天晚上 11:59:59.999(即 23:59:59) )。

对于这两个日期,对于每个交易集合,我们调整 query如下,调用toDate()方法:

db.collection("shops")
.doc(shopDoc[j].key)
.collection("transactions")
.orderBy("createAt")
.where("createAt", ">", m1.toDate())
.where("createAt", "<=", m2.toDate())
.get();

就是这样。这里最大的优势是过滤是在 Firestore 数据库中完成的(在后端),而不是像您在问题中所做的那样在前端(if (transactionList[l].data.createAt < currentTime){...})

关于node.js - 如何查询 Firestore 今天所有文档的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59300939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com