Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

javascript - Cloud functions not offsetting date for firestore query

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

The browser seems to set the hours to 6 and 12 appropriately but on the cloud function it remains at 2 and 8.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...