I'm making a request with a for of loop to delete all the cart elements, but it doesn't work as expected. Loop works for first iteration but API returns an error in the second iteration.
I think it's a logical error, what's the problem with this code?
My try:
module.exports.removeAllCartItems = (token) => {
this.getCart(token, (error, data) => {
if(error){
Sentry.captureException(error);
}
const cartItems = data.items
console.log('------cart items----------')
console.log(cartItems)
console.log('------cart items----------')
for(item of cartItems){
let productId = item.productId
let variantId = item.variant.product_id
console.log('--------item one by one---------')
console.log(item)
console.log('--------item one by one---------')
this.removeItem(token, productId, variantId, (error, data)=> {
if(error) {
Sentry.captureException(error);
}
console.log('------data-------')
console.log(data)
console.log('******data*******')
} )
}
})
}
Remove item method:
module.exports.removeItem = (token, productId, variantId, callback) => {
const url = urlBase + "/removeItem" + `?secretKey=${secretKey}`;
request(
{
url: url,
method: "DELETE",
json: true,
headers: {
"content-type": "application/json",
Authorization: `Bearer ${token}`,
},
body: {
secretKey: `${secretKey}`,
productId: productId,
variantId: variantId,
},
},
(error, response) => {
if (error) {
Sentry.captureException(error);
callback(errMessage, undefined);
} else {
const data = response.body;
callback(undefined, data);
}
}
);
};
Console output:
------cart items----------
[
{
variant: {
variation_values: [Object],
price: 475,
product_id: '883360541280',
orderable: true
},
_id: '62403a55ec05ea0024e9d965',
productId: '21736758',
quantity: 1
},
{
variant: {
variation_values: [Object],
price: 58.99,
product_id: '701643411498',
orderable: true
},
_id: '62403b7eec05ea0024e9d966',
productId: '25589208',
quantity: 1
variation_values: { color: 'P2V', size: '42' },
price: 475,
product_id: '883360541280',
orderable: true
},
_id: '62403a55ec05ea0024e9d965',
productId: '21736758',
quantity: 1
}
--------item one by one---------
--------item one by one---------
{
variant: {
variation_values: { color: 'JJ3WCXX', size: '9MD' },
price: 58.99,
product_id: '701643411498',
orderable: true
},
_id: '62403b7eec05ea0024e9d966',
productId: '25589208',
quantity: 1
}
--------item one by one---------
------data-------
{
_id: '6240398eec05ea0024e9d962',
secretKey: '<secretKey>',
userId: '623dfb884083b000243cc4a7',
items: [
{
variant: [Object],
_id: '62403a55ec05ea0024e9d965',
productId: '21736758',
quantity: 1
}
],
__v: 6
}
******data*******
------data-------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Internal Server Error</pre>
</body>
</html>
******data*******
Why API returns internal server error in the second iteration, is it about my code or something else, any idea ?
I make it work with that way but still dont know why first implementation doesn't work
module.exports.removeAllCartItems = (token) => {
this.getCart(token, async (error, data) => {
if (error) {
Sentry.captureException(error);
}
const cartItems = data.items;
for (item of cartItems) {
let productId = item.productId;
let variantId = item.variant.product_id;
await new Promise((resolve, reject) => {
this.removeItem(token, productId, variantId, (error, data) => {
if (error) {
reject(error);
Sentry.captureException(error);
}
resolve(data);
});
});
}
});
};
thisreferring to in your function? That's a bug waiting to happen.module.exports.removeAllCartItems = function ...that meansexportscan't be a class. And whenexportsis not a class, thenthisis not going to be bound to anything useful insideremoveAllCartItems, or requires explicit binding via.call()or.apply(). So if it works for you, fine. I think it looks fishy.