I'm currently working on adding a feature to my app where I can take the products from an order and send them to the cart, essentially replicating the entire order.
While I have successfully retrieved the order, I am facing difficulty in sending it back to the API using the POST method.
The API call in the cart.service.ts file is as follows:
repeatOrder(products: SingleOrder['products']) {
return from(Preferences.get({key: 'TOKEN_KEY'})).pipe(
switchMap(token => {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token.value}`);
const formData: any = new FormData();
formData.append('products', products);
return this.httpClient.post(`${environment.apiUrl}cart/repeatorder`, formData, {headers, observe: 'response'});
}),
catchError(err => {
console.log(err.status);
if (err.status === 400) {
console.log('Error 400: ', err.error.message);
}
if (err.status === 401) {
this.authService.logout();
this.router.navigateByUrl('/login', {replaceUrl: true});
}
return EMPTY;
}),
);
};
Additionally, here is the repeat purchase function in the order-view.page.ts file:
repeatThisPurchase() {
this.repeatOrderArr= [...this.orderProducts];
this.cartService.repeatOrder(this.repeatOrderArr).subscribe(
data => {
console.log('Data sent to cart: ', data);
},
error => {
console.log('Error', error);
}
);
}
Lastly, here is the button I utilize to invoke the repeatPurchase function:
<div class="btn-wrapper">
<ion-button color="vigros" class="purchase-btn" size="default" type="submit" (click)="repeatThisPurchase()" expand="block">Ponovi nakup</ion-button>
</div>
Upon inspection of the console in my browser, I am encountering a 500 error, and the payload in the Networks tab displays: products: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Could you point out where I might be going wrong?