Reputation: 87
I need to retrieve data by comparing the current date to a property named start date in my firestore database. I only need to retrieve the data that matches the current date value with the start date. I have created a query to retrieve the data but I am unable to retrieve any data using that query.
event.model.ts
export interface Events{
title:String;
start:Date;
end:Date;
}
app.component.ts
event$:Observable<Events[]>;
const newDate = firebase.firestore.Timestamp.fromDate(new Date()).toDate();
this.event$ = this.firestore.collection<Events>('eventsCalender',
ref => ref.where('start','==',newDate)).valueChanges();
this.event$.subscribe(data => console.log(data));
I have created an interface and exported, and in the app.component.ts the query to retrieve the event is implemented. start is the start date variable where I check whether the start variable is equal to the current date variable, if so return those events else there are no current day events. But the code I have written fetches no data even when there are data in the database.
Upvotes: 0
Views: 2131
Reputation: 599581
When you store a date in Firestore, it is stored with microsecond granularity. When you create a new Date()
in JavaScript, it has millisecond granularity. The chance that two such dates that are created at separate moments are exactly equal is really small.
You'll typically want to do one of these:
var newDate = new Date(); newDate.setHours(0,0,0,0);
to compare midnights to midnights.Query for a range. For example: to get all fields with a start date of today, you could do:
var startOfToday = new Date();
startOfToday.setHours(0,0,0,0);
var endOfToday = new Date();
endOfToday.setHours(23,59,59,999);
ref.where('start','>=',startOfToday).where('start', '<=', endOfToday)
Upvotes: 1