Andrew Burns
Andrew Burns

Reputation: 346

Comparing Date types Angular 5 - getDate() not a function

Sorry for the very generic wording of the title I couldn't think of a better way to describe what I'm trying to do.

code below are highlights of my home.component.ts

completedOrderTotal: Order[] = [];
completedOrderDay: Order[] = [];

 getCompletedOrders() {
    this.loadingCompletedOrders = true;
    var totalCompletedOrders = 0;
    var now = new Date();
    this.orderService.get(6).subscribe((order) => {
        this.completedOrderTotal = order.filter((c) => c.COMPLETED == true);
        this.completedOrderDay = order.filter((d) => d.COMPLETED == true && d.DATE_COMPLETED.getDay() < now.getDay()-1);
        this.totalCompletedDay = this.completedOrderDay.length;
        this.totalCompletedOrderTotal = this.completedOrderTotal .length;
        this.loadingCompletedOrders = false;

    });
}

export class Order {
   BUY_ORDER_ID: number;
   SELL_ORDER_ID: number;
   COMPLETED: boolean;
   BUY_TOTAL: number;
   SELL_TOTAL: number;
   BUY_TRIGGER: string;
   DATE_CREATED: Date;
   DATE_COMPLETED: Date;
}

order.service.ts (API Service)

 @Injectable()
export class OrderService {
constructor(private http: HttpClient) { }

   get(id: number) {
       return this.http.get<Order[]>('api/order/' + id);
   };
}

I want to have a 2 objects holding 2 sets of data. 1. All Orders that have completed 2. All Orders that have completed in the last 24 hours

i'm struggling with this line:

 this.completedOrderDay = order.filter((d) => d.COMPLETED == true && d.DATE_COMPLETED.getDay() < now.getDay()-1);

I get the error d.DATE_COMPLETED.getDay() is not a function.

Can anyone point me in the right direction here?

Cheers,

Upvotes: 0

Views: 1039

Answers (1)

JB Nizet
JB Nizet

Reputation: 691865

You're getting JSON from your backend using the HttpClient. JSON doesn't have a Date type. It has strings, booleans, numbers, arrays and objects. So an Order can't possibly have a field of type Date.

Also, you should really improve your naming conventions. The method get() taking an ID as argument, getting from a URL api/order/{id}, and the name order all suggest that what is returned is a single order. And yet it seems to be an array of orders.

Finally,

order.filter((c) => c.COMPLETED == true);

should simply be

order.filter(c => c.COMPLETED);

Upvotes: 1

Related Questions