I have encountered an issue with my query that is supposed to fetch values between the start and end of the month. Interestingly, when a record is entered on the first day of the month, it doesn't get returned in the query result. Only records entered after the first day of the month are being returned.
In the Cloud Firestore database, I am sending an object of type Date()
. However, upon saving, it gets converted to a Timestamp
, causing the returned records to be of type Timestamp
instead of Date()
.
I have attempted to use the getTime()
method in the query but unfortunately, no records are retrieved.
The recorded date in the database is:
February 1, 2019 00:00:00 UTC-3
If I modify the time, then the record is fetched along with others.
February 1, 2019 01:00:00 UTC-3
To save the date, I am using mat-datepicker from Angular Material. This component only saves the date without the time.
Although I have tried setting the minutes every time a record is entered, this practice causes the last day of the month's record to be excluded from the results.
Below is the snippet of code where I am executing the query:
selectedMonth$: BehaviorSubject<{ startOf: Date, endOf: Date }>;
startOfMonth = moment(new Date()).startOf('month').toDate();
endOfMonth = moment(new Date()).endOf('month').toDate();
this.selectedMonth$ = new BehaviorSubject({
startOf: this.startOfMonth,
endOf: this.endOfMonth
});
this.transactions$ = this.selectedMonth$.pipe(
switchMap(dateSelected => this._angularFirestore.collection(collectionRef, ref =>
ref.where('dueDate', '>', dateSelected.startOf)
.where('dueDate', '<', dateSelected.endOf))
.snapshotChanges()
.pipe(
map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as any;
data.id = a.payload.doc.id;
return data;
});
})
)));
updateSelectedMonth(date: Date): void {
this.startOfMonth = moment(date).startOf('month').toDate();
this.endOfMonth = moment(date).endOf('month').toDate();
this.selectedMonth$.next({ startOf: this.startOfMonth, endOf: this.endOfMonth });
}