I am having the hardest time trying to code what should be a straight forward piece of logic. I have searched stackoverflow and the "interconnect pipes" but all the solutions don't seem to work for me. I am using Angular 8 and Visual Studio Code for development. I have a document in Firestore with a timestamp field:
In my model file I have the startDate declared as a Date
export interface Fundraiser {
fundId: string;
name: string;
startDate: Date;
endDate: Date;
imagePath: string;
fundsRaised: number;
items: Product[];
}
in my component.ts file I retrieve the document from firestore
ngOnInit() {
this.fundraiserService.getFundraisers().subscribe(fundraisers => {
this.fundraisers = fundraisers;
console.log('Fundraiser ', this.fundraisers);
});
}
In my HTML I have a typical for loop *ngFor="let fundraiser of fundraisers". Then I call a method to check if the start and end dates are within a range, passing in the timestamps as parameters.
<h5 class="list-group-item-heading text-success" *ngIf="isActive(fundraiser.startDate, fundraiser.endDate)">Currently active</h5>
<h5 class="list-group-item-heading text-secondary" *ngIf="!isActive() && !isStaged()">Finished</h5>
So now for my method isActive():
isActive(startDate: Date, endDate: Date) {
if (startDate < new Date().getTime() && endDate > new Date().getTime()) {
return true;
}
return false;
}
The code does not compile. The error is:
"Operator '<' cannot be applied to types 'Date' and 'number'."
I have also tried using the toDate() method with no luck.
isActive(startDate: Date, endDate: Date) {
if (startDate.toDate() < new Date() && endDate.toDate() > new Date()) {
return true;
}
return false;
}
This compiles, but the I get an error in the HTML
ERROR TypeError: Cannot read property 'toDate' of undefined at FundraiserListComponent.isActive (fundraiser-list.component.ts:36)
When I first tried to use the method toDate() I received a typescript error, I had to add it to the "properties" (via quick fix) to get the code to compile. I have also tried numerous other solutions, to many to list here.
Any help would be great. I have been struggling with this for a couple of days now.