1
votes

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.

Example data from document

Am I missing something?

1
It may be the currentDate that is not the same as your local version ?CR7
Have you tried logging the date variables in play to see if they are what you expect them to be? Without knowing that, we have no idea if your query is actually going to find the thing you're looking for - everything is hidden behind variables and document fields we can't see.Doug Stevenson
@DougStevenson Sorry about that, updated the question with the date output and a screenshot of the data it's checking.Jason

1 Answers

2
votes

You have a problem with timezone differences. Your date from moment is measured at UTC (that's what the "Z" means at the end of the logged currentDate string), but your date in the timestamp field is measured at UTC+1. They are both saying "midnight", but since they are in different timezones, they are not referring to the same moment in time, so your query does not match.

You will need to take some care to make sure the exact same moment in time is being used in both the query and the document. I suggest also taking a look at the code that inserts the date in the timestamp to get it to match what the query wants.