The query fails to retrieve results for the specified date and the beginning of the month

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 });
}

Answer №1

Revise:

ref.where('dueDate', '>', dateSelected.startOf)
   .where('dueDate', '<', dateSelected.endOf))

Modify to:

ref.where('dueDate', '>=', dateSelected.startOf)
   .where('dueDate', '<=', dateSelected.endOf))

Make sure that dateSelected.endOf encompasses the entire selected date, including the latest possible time (23:59:59) or just set it to the start of the next day at 0:00 and:

ref.where('dueDate', '>=', dateSelected.startOf)
   .where('dueDate', '<', dateSelected.endOf))

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to position the label to the left side of the gauge?

Is there a way to position the zero number outside the gauge? I'm having trouble figuring out how to do it because the x & y options won't work since the plotLine's position keeps changing. The zero needs to move along with the plotLine and ...

Change the class of <body> when the button is clicked

One of my tasks involves adding a button that, when clicked, should give the body the class "open-menu". Implementing this using jQuery was quite straightforward - I just needed to add the following line of code: $('.burger').click(function() ...

What is the best way to incorporate dynamic data from Firestore into a function using the `where()` method, while also utilizing the `snap.size` property to accurately count the total number of queries

I am facing an issue while trying to fetch data dynamically from firestore using a where() function. The specific error message I encountered is: TypeError: vaccines is not a function The user collection: [![enter image description here][1]][1] Here a ...

multer failing to upload files using the default example

I am encountering difficulties with getting my code to function properly and I am not receiving any errors from multer. I have thoroughly double-checked everywhere for potential mistakes, but I am still stuck. Any assistance would be greatly appreciated. ...

Constructing a hierarchical tree structure using an array of objects that are initially flat

My goal is to create a hierarchical tree structure from a flat array: The original flat array looks like this: nodes = [ {id: 1, pid: 0, name: "kpittu"}, {id: 2, pid: 0, name: "news"}, {id: 3, pid: 0, name: "menu"}, {id: 4, pid: 3, name: ...

The type 'any' cannot be assigned to the type 'never' as a parameter

const [files, setFiles] = useState([]) const handleChange = (event: any) => { setFiles.push(event.target.files[0].name) return (<div> {files.map((file: any) => ( <p>Hello!</p> ))} </ ...

Issue with starting @mauron85/cordova-plugin-background-geolocation on Ionic 5 and Angular 9 platform

I'm facing a challenge with integrating the background geolocation plugin into my app. Here is the documentation link for reference: https://ionicframework.com/docs/native/background-geolocation Here's the snippet of my code that initiates the p ...

Creating a PDF export of your grid using Kendo Grid is a straightforward

I've been facing a challenge while trying to export an entire page using Kendo Angular PDF. Everything works smoothly until I add a Kendo Angular Grid onto the page. The problem arises when certain rows go missing and extra blank space appears on some ...

Having trouble with Angular's ActivatedRoute and paramMap.get('id')?

Currently, I am attempting to retrieve information from my server using the object's ID. The ID can be found in the URL as well: http://xyz/detail/5ee8cb8398e9a44d0df65455 In order to achieve this, I have implemented the following code in xyz.compo ...

Using React to update the state of an array of objects

I'm faced with a challenge in changing the properties of an object within an array of objects at a specific index using a function: const handleEdit= (index) =>{ if(itemList[index].edit==true){ const copied=[...itemList]; const item2 = {...ite ...

How can we eliminate the need for specifying the order of generic arguments in TypeScript?

In the development of my middleware engine, I have incorporated various generic arguments that are specific to the particular implementation in use. export type Middleware< Store = never, Args = unknown, Response = unknown > = ( context: { ...

Is it possible to access the attributes of an interface in TypeScript without relying on external libraries?

Ensuring the properties of an interface align with an object that implements it is crucial for successful unit testing. If modifications are made to the interface, the unit test should fail if it is not updated with the new members. Although I attempted ...

Is there a way for me to trigger the opening of a modal component through code?

Is there a way to programmatically open a modal similar to a modal service? In Angular 1, I used the following code: uibModal.open({template:'url.html', controller: MyController}). This method allowed me to avoid adding modal HTML code to my par ...

Exploring the dynamic duo of SystemJS and AngularJS 2

I am currently working on integrating the Core Angular2 module into my application, which is written in Typescript. It's essentially following the same structure as the quick start tutorial on the Angular.IO website. However, I am facing a challenge ...

Issues encountered when attempting to utilize ng-autocomplete 2

Attempting to implement ng2-autocomplete by following the documentation at https://www.npmjs.com/package/ng2-completer I have been encountering various errors every time I attempt solutions from Stack Overflow or GitHub. Any assistance in resolving this i ...

Ways to verify if the current date exists within a TypeScript date array

I am trying to find a way in typescript to check if the current date is included in a given array of dates. However, even after using the code below, it still returns false even when the current date should be present within the array. Can anyone please pr ...

Getting response headers in Angular by utilizing an HTTP interceptor

Seeking guidance on how to interpret response headers when utilizing httpinteceptor in Angular. I have exposed all the tokens in my Node.js application, but am facing challenges in comprehending all the keys passed in the response headers. intercept(req: ...

Can a JavaScript object be created in TypeScript?

Looking for a way to utilize an existing JavaScript "class" within an Angular2 component written in TypeScript? The class is currently defined as follows: function Person(name, age) { this.name = name; this.age = age; } Despite the fact that Java ...

Tips for securely passing props based on conditions to a functional component in React

I came across this situation: const enum Tag { Friday: 'Friday', Planning: 'Planning' } type Props = { tag: Tag, // tour: (location: string) => void, // time: (date: Date) => void, } const Child: React.FC<Props> = ...

Incorporating Bloodhound into an Angular 2 CLI project

I have been working on integrating Bloodhound.js into an Angular 2 project that utilizes Angular 2 CLI. Currently, I have successfully implemented jQuery by following these steps: Installed jQuery using npm install jquery --save Installed jQuery Type ...