niclas_4
niclas_4

Reputation: 3674

Firestore - Query via Timestamp doesn´t return results

I wanna use the Timestamp as a Condition in my Query and end up with the following inside of my Cloud Function:

var dateRange = addMinutes(new Date(), 195); // 120 = timezone diff
dateRange = getTime(dateRange);

console.log(dateRange); // --> 1562933996501

const test = await fs.collection('tournaments').doc('B37tIBzDQDFXndv24KHs').get()

console.log(test.data().start); --> Timestamp { _seconds: 1562923800, _nanoseconds: 0 },

const queryRef = fs.collection('tournaments').where('status', '==', '0').where('start', '<=', dateRange);

But for some reason the querySnapshot´s size is 0, while the console.logs say that the variable dateRange = 1562933996501 and the test timestamp I query is 1562923800, so it should be in the area or not?

I use the timestamp field in Firestore for my timestamp. And yes the status of the document is 0.

EDIT trying this out:

console.log(test.data().status == 0) // = true
console.log(test.data().start.seconds < dateRange); // = true

Upvotes: 0

Views: 232

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317878

It looks like you're trying to query a timestamp type field with an integer. That won't work. You can only query timestamps type fields with Timestamp type objects (as you output shows that you've gotten from the first query). Or, in JavaScript, you can pass a Date object, which will get converted to a Timestamp automatically.

Upvotes: 3

T. Dayya
T. Dayya

Reputation: 788

You can also chain multiple where() methods to create more specific queries (logical AND). However, to combine the equality operator (==) with a range or array-contains clause (<, <=, >, >=, or array-contains), make sure to create a composite index.

This is from firestore's documentation. Look under "compound queries."

Upvotes: 0

Related Questions