简体   繁体   中英

Property 'id' does not exist on type 'never'. angular

Hi I'm practicing on a small store website project this one but I'm having a problem with moving the items to the cart.

in the source code in file app/components/product-list/product-list.component.ts line 33 :

const cartIdx = cartProducts.findIndex(cart => cart.id === cartProduct.id)

when I do the same line I face the error: Property 'id' does not exist on type 'never'

that's my code:

  onSubmit(cartItem: Product){
let toCartItem: CartItems[] = [];
let alertMessage: string = '';

const itemCount = this.selectedOption;
const cartItems: CartItems[] | [] = this.ProductserveService.getCartItems();


const itemID = cartItems.findIndex(cart => cart.id === cartItem.id)
toCartItem = cartItems;

if((itemID === -1) || (cartItems.length === 0)){
  toCartItem.push(Object.assign(cartItem, {option: itemCount}));

  alertMessage = `New Item '${cartItem.name}' added to cart`;
}

this.ProductserveService.addToCart(toCartItem);

alert(alertMessage);

this.printLocalData();
return false;}

anyone know why is that ?

我已经浏览了您的代码,您错过了 findIndex 中购物车的数据类型,将其const cartIdx = cartProducts.findIndex(cart: CartProduct => cart.id === cartProduct.id) from const cartIdx = cartProducts.findIndex(cart => cart.id === cartProduct.id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM