0
votes

I work on an e-commerce site and I have errors in my "cartservice" and in particular on my "checkoutFromCart ()" function, the error that the console displays is as follows:

src/app/services/cart.service.ts:218:81 218 this.http.post(${this.serverUrl}orders/payment, null).subscribe((res: { success: boolean }) => { ~~~~~~~ 'success' is declared here.

Error: src/app/services/cart.service.ts:228:24 - error TS2769: No overload matches this call. Overload 1 of 5, '(observer?: NextObserver | ErrorObserver | CompletionObserver | undefined): Subscription', gave the following error. Argument of type '(data: orderResponse) => void' is not assignable to parameter of type 'NextObserver | ErrorObserver | CompletionObserver | undefined'. Property 'complete' is missing in type '(data: orderResponse) => void' but required in type 'CompletionObserver'. Overload 2 of 5, '(next?: ((value: Object) => void) | undefined, error?: ((error: any) => void) | undefined, complete?: (() => void) | undefined): Subscription', gave the following error. Argument of type '(data: orderResponse) => void' is not assignable to parameter of type '(value: Object) => void'. Types of parameters 'data' and 'value' are incompatible. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Type 'Object' is missing the following properties from type 'orderResponse': order_id, success, message, products

228 }).subscribe((data:orderResponse) => { [91m ~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/rxjs/internal/types.d.ts:73:5 73 complete: () => void; ~~~~~~~~ 'complete' is declared here.

my code: checkoutFonction

console error: error

I'm sorry if the question is wrong to ask I'm starting on stackoverflow but please I need help

I'm sorry for the images here is the code:

CheckoutFromCart(userId: number) {

  this.http.post(`${this.serverUrl}orders/payment`, null).subscribe((res:{ success: boolean }) => {
    console.clear();

    if (res.success) {


      this.resetServerData();
      this.http.post(`${this.serverUrl}orders/new`, {
        userId: userId,
        products: this.cartDataClient.prodData
      }).subscribe((data:orderResponse) => {

        this.orderservice.getSingleOrder(data.order_id).then(prods => {
          if (data.success) {
            const navigationExtras: NavigationExtras = {
              state: {
                message: data.message,
                products: prods,
                orderId: data.order_id,
                total: this.cartDataClient.total
              }
            };
            this.router.navigate(['/thankyou'], navigationExtras).then(p => {
              this.cartDataClient = {prodData: [{incart: 0, id: 0}], total: 0};
              this.cartTotal$.next(0);
              localStorage.setItem('cart', JSON.stringify(this.cartDataClient));
            });
          }
        });

      })
    } else {
      this.router.navigateByUrl('/checkout').then();
    }
  })
}
1
getSingleOrder is a Promise or an observable? BTW you can use rxjs operator switchMap or another to "concat" observables - Eliseo
Try not to post images of code, rather just paste the code here. - Dane Brouwer

1 Answers

0
votes

You can define the return type in your post methods like this:

this.http.post<{success: boolean}>().subscr...

and

this.http.post<orderResponse>().subscr...