Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Property 'id' does not exist on type 'Order[]'

Getting this strange error in my HTML that says property ‘id’ does not exist on type Order[] when it does very explicitly exists

The error: Property 'id' does not exist on type 'Order[]'

Here is the HTML:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div class="admin-page" *ngIf="order">
  <p-card [header]="'View Order'" subheader="You can edit order status here">
          <h5>Order Id</h5>
          <p>{{ order.id }}</p> //HERE I AM GETTING THE ERROR
        </div>

Here is the relevant ts:

export class OrderDetailComponent {
  order: Order[] = [];

private _getOrder() {
    this.route.params.subscribe((params) => {
      if (params['id']) {
        this.orderService
          .getOrder(params['id'])
          .subscribe((order) => {
            this.order = order;
            console.log(this.order);
          });
      }
    });
  }

}

And the orde.ts file:

/* eslint-disable @typescript-eslint/no-explicit-any */
import { User } from '@eshop-repository/users';
import { OrderItem } from './order-item';

export class Order {
  
  id?: string;
  orderItems?: OrderItem[];
  shippingAddress1?: string;
  shippingAddress2?: string;
  city?: string;
  zip?: string;
  country?: string;
  phone?: string;
  status?: number;
  totalPrice?: string;
  user?: User;
  dateOrdered?: string;
  _id?: string;
  
}

And the order-item.ts file:

/* eslint-disable @nrwl/nx/enforce-module-boundaries */
import { Product } from "@eshop-repository/products";

export class OrderItem {
    product?: Product;
    quantity?: number;
  }

The error I am getting is that id doesn’t exist on Order, when you can see it very clearly does. Order is an array, maybe that is causing the problems?

Any help would be appreciated.

>Solution :

You declared order as an array of Order (Order[])

Simply change your declaration to

export class OrderDetailComponent {
  order: Order; //instead of Order[]
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading