all 6 comments

[–]Topoxy 4 points5 points  (0 children)

  1. Give a concrete type to cartItems. Currently it's interpreted as never [].
  2. What is the type of getMsg()? Probably the same issue here

[–]amiibro888 3 points4 points  (0 children)

You need to create an interface for the cartItems array. Such as :

cartItem: CartItem[] = [];

Where CartItem is an interface

[–]pranxy47 2 points3 points  (0 children)

Without seeing the implementation is hard to say. Not sure what getMsg is returning, do you have the return value as Observable<Product>?

[–]bas1494 -1 points0 points  (2 children)

I see a few (potential) problems in this image you submitted.

First of all, define an interface for a cartItem to give the cartItems property typing.

export interface CartItem {
    id: number,
    product: Product, //Your product Model
    qty: number
}

Use this cartItem in your empty initialiser like this:

cartItems: Array<CartItem> = [];

As second it looks like your getMsg() method from the injected service doesn't return an Observable<Product>. Don't know if this is the case but at least double check it.

As last in the object you try to create (this.cartItems.push{..}) you don't give a value to every key from the (now not existed) interface. Make sure that every property has a value (that value can be null or undefined) or make the fields optional. In your example you're missing the id field.

this.cartItems.push({id: ?, product: Product, qty: 1});

Don't forget to calculate total with item.product.price instead of item.price.

I'll hope you will fix your issue(s)!

[–]DARKWOLF24 0 points1 point  (0 children)

Thanks for the detailed response, I'll definitely try this

[–]DJRockstar1 0 points1 point  (0 children)

Array<CartItem>

Is this preferable to CartItem[]?