Use array.forEach to modify resultset from sequelize.
I want to loop over the shoppingCart returned, access database for each element and add some properties and return.
const promiseInvoices = [];
await ShoppingCart.findAll({
where: {
// eslint-disable-next-line object-shorthand
cart_id: cartItem.cart_id,
},
}).then(result => {
result.forEach(cartItem2 => {
const prod = Product.findByPk(cartItem2.product_id);
console.log(prod);
let cartItem3 = {};
const total = prod.price * cartItem2.quantity;
cartItem3.cart_id = cartItem2.cart_id;
cartItem3.product_id =cartItem2.product_id;
cartItem3.attributes =cartItem2.attributes;
cartItem3.quantity =cartItem2.quantity ;
cartItem3.price = prod.price;
cartItem3.name = prod.name;
cartItem3.image = prod.image2;
cartItem3.item_id = cartItem2.item_id;
cartItem3.subtotal = total;
return promiseInvoices.push(cartItem3);
});
});
Return the shoppingCart, access the database for each shoppingcart object and get the product to fill in the values of each shoppingCart item. The call to Product.findByPk is returning an usable promise like this:
Promise [Object] {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }