I have a pub/sub function running which when triggered, fetches all documents from a Firebase collection where date
is today.
Tested the code in a local (non-pubsub) node environment and works perfectly, but when published, it cannot find any documents, but can in the local version.
I'm guessing it's something to do with date passed, but I can't see why it would work locally but not in this pub/sub function?
export const check_orders = functions.pubsub.schedule('0 9 * * *').timeZone('Europe/London').onRun(async context => {
const currentDate = moment();
currentDate.set({hour: 0, minute: 0, second: 0, millisecond: 0});
console.log('currentDate:', currentDate);
const toDate = currentDate.toDate();
console.log('toDate:', toDate);
const currentDateTimestamp = admin.firestore.Timestamp.fromDate(currentDate.toDate());
console.log('currentDateTimestamp:', currentDateTimestamp);
const query = db.collection('orders').where('delivery.delivery_due', '==', toDate).where('user_status', '==', 'active');
const tasks = await query.get();
...
}
When this is triggered, currentDate
will log: moment("2020-05-01T00:00:00.000")
I have also tried adding toDate()
to currentDate
which will log 2020-05-01T00:00:00.000Z
. Also is unable to find any documents.
Added another test to convert the date to a Firebase timestamp which outputs Timestamp { _seconds: 1588291200, _nanoseconds: 0 }
, but still does not fetch any documents.
If I loop through the above code in the local test, I would get back 1 document, but the pub/sub will not return any. Tested removing only fetching documents where status = active
and it's successful, so it's for certain to do with the date.
The test document delivery_due
date is set to today and the user_status
is set to active
, so the data in the document is as expected.
Am I missing something?