云功能无法抵消Firestore查询的日期 [英] Cloud functions not offsetting date for firestore query
问题描述
我有一份cron作业,每天运行@ 12PM.它应该获取当天上午6点到下午12点之间添加的集合中的所有文档.我正在使用名为 dateOrderAdded
的字段来运行查询.该字段位于GMT -4时区,所以当我运行查询时,我必须满足该要求.
I have a cron job that runs @12PM everyday. It's supposed to grab all the documents in a collection that were added between 6AM and 12PM on that day. I'm using a field called dateOrderAdded
to run the query on. This field is in the timezone GMT -4 so when I'm running the query I have to cater for that.
"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.sendTwelvePmOrderSummary = functions.pubsub.schedule("0 12 * * *")
.timeZone("America/Caracas")
.onRun(async context => {
//Get current date
const today = new Date();
//Set time to 6AM
const todaySixAm = new Date(today.setHours(6,0,0,0));
//Set time to 12PM
const todayTwelvePm = new Date(today.setHours(12,0,0,0));
console.log(todaySixAm.valueOf()); //browser - 1609149600000 | cloud fn - 1609135200000
console.log(todayTwelvePm.valueOf()); // browser - 1609171200000 | cloud fn - 1609156800000
//Get all orders in-between today at 6AM and today at 12PM
const orderCol = await admin.firestore().collection('orders').where('dateOrderAdded','>=',todaySixAm).where('dateOrderAdded','<=',todayTwelvePm).get();
const orderDocs = orderCol.docs.map(doc=>doc.data())
if(orderCol.size === 0 ){
//No orders placed - TODO: Send no orders placed email
console.log('No orders placed');
}else{
//orders! - TODO: Send order placement summary
console.log('Orders placed');
}
return null;
}
浏览器似乎将小时数适当地设置为6和12,但是在云功能上,它保持在2和8.
The browser seems to set the hours to 6 and 12 appropriately but on the cloud function it remains at 2 and 8.
由于日期是云函数中的UTC,因此第2小时和第8小时是有意义的,但是我将小时数明确地设置为6和12,因此我不确定为什么看不到设置的更改.
Since dates are UTC in cloud functions the hours 2 and 8 make sense but I explicitly set the hours to 6 and 12 and I'm not sure why I'm not seeing the change being set.
推荐答案
在@RobG的建议中,我将其用于setUTC小时.
Taking the suggestion from @RobG in the comments I got it to work with setUTC hours.
"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
exports.sendTwelvePmOrderSummary = functions.pubsub
.schedule("0 12 * * *")
.timeZone("America/Caracas")
.onRun(async context => {
//Timezone of stored data is GMT -4
//For query to be run correctly use UTC hours
//6AM = 6+ 4 = 10 UTC hours
const todaySixAm = new Date(new Date().setUTCHours(10,0,0,0));
//Set time to 11:59:59:999AM - 12 + 4 = 16
//Set it to just before 12PM so results from other CRONs won't be duplicated
const todayTwelvePm = new Date(new Date().setUTCHours(15, 59, 59,999));
//Get all orders in-between today at 6AM and today at 12PM
const orderCol = await admin.firestore().collection('orders').where('dateOrderAdded','>=',todaySixAm).where('dateOrderAdded','<=',todayTwelvePm).get();
const orderDocs = orderCol.docs.map(doc => doc.data());
if (orderCol.size === 0)
return { message: "No orders placed - TWELVE PM Summary" };
console.log('Orders placed!');
return null;
}
这篇关于云功能无法抵消Firestore查询的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!