1

From CartComponent I am calling the removeItem() function in CartService.

CartComponent#removeItem():

public removeItem(itemId:string) {
    this.cartService.removeItem(itemId).subscribe(cart => {
      console.log('CartComponent#removeItem', )
      this.cartItems = cart.cartItem;
    });
  }

Typescript is giving the following error for cart.cartItem:

[ts] Property 'cartItem' does not exist on type 'void'.
any

I have done precisely this in another application and it works just fine.

CartService#removeItem()

public removeItem(itemId:string) {
   console.log('CartService#removeItem', itemId)
   return this.httpClient.delete<Cart>(`${this.cartItemUrl}${itemId}`)
   .pipe(
     tap(cart => console.log('cart@removeItem', cart)),
     // map(cart => this.cart = cart, this.setItemCount()),
     map(cart => {
       this.cart = cart;
       this.setItemCount();
     }),
     catchError(this.handleError.bind(this))
   )
 }

1 Answer 1

2

The map function is used to transform the emitted value of the Observable. Whatever is returned from map will be the emitted value.

If your case, nothing is returned from your map function, so the subscribe in CartComponent has no way of accessing the value.

Sign up to request clarification or add additional context in comments.

3 Comments

I do have a return statement in CartService#removeItem and this worked in other codes.
No, you need another one inside the map function, underneath this.setItemCount() you probably want return this.cart as well. If map doesn't return anything your observable will emit undefined
just a side note, use map when you are trying to map the response, in example, transform it to something else. what you did inside the map has nothing to do with the response transformation, hence, do it inside the tap. so the response won't change, but you may want to do other stuff related to the response inside the service itself (like your console.log)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.