1
votes

I am having a TypeScript error:

Argument of type '(data: Products) => void' is not assignable to parameter of type '(value: Products[]) => void'. Types of parameters 'data' and 'value' are incompatible. Type 'Products[]' is not assignable to type 'Products'. Property 'id' is missing in type 'Products[]'.

This is my schema:

export class Products {
  id: number;
  prodImage: any;
  catid: number;
  catId: number;
}

this is my ts code where I'm getting the error:

import { Component, OnInit } from '@angular/core';
import { Router, Params, ActivatedRoute  } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import {Products } from '../Admin/products/products';
import {ProductsService } from '../Admin/products/products.service';


@Component({
  selector: 'app-cart',
  templateUrl: './cart.component.html',
  styleUrls: ['./cart.component.css']
})
export class CartComponent implements OnInit {
  sess_id: any;
  prods: Products;
  cartList: Products;

  constructor(
    private router: Router,
    private productsService: ProductsService,
    private routes: ActivatedRoute,
  ) { }

  ngOnInit() {
    this.sess_id = localStorage.getItem('sess_key');

    this.productsService.viewCart(this.sess_id)
    .subscribe((data: Products) => {      //getting error over here
      this.cartList = data;
      console.log(this.cartList);

    });}}

Where am I going wrong?? Thanks in advance

1
The productsService.viewCart returns a Observable<Products[]> not a Observable<Product>Poul Kruijt

1 Answers

2
votes

You are getting an Products array from your service, not a Products element. Set cardList to: cartList :Products[]