I have an association many to many with two tables, products and orders. In my pivot table i save the id, quantity and price of the product. When I fetch the product i need the name of this product, but to get the name I need to get in the product table. The response of my fetching return like this
{
"id": 111,
"name": "Matheus",
"phonenumber": "69993750103",
"reference": null,
"value_subtotal": "10.000",
"value_delivery": "5.000",
"value_total": "15.000",
"status": "pending",
"products": [
{
"name": "Açai 350ml",
"OrdersProducts": {
"quantity": 2,
"price": "0.000"
}
},
{
"name": "acai 350ml",
"OrdersProducts": {
"quantity": 3,
"price": "0.000"
}
}
]
}
but i need the json in this format
{
"id": 111,
"name": "Matheus",
"street": "Rua olavo bilac",
"phonenumber": "69993750103",
"number": "3511",
"reference": null,
"note": "Retirar o morango",
"value_subtotal": "10.000",
"value_delivery": "5.000",
"value_total": "15.000",
"status": "pending",
"createdAt": "2021-10-20T18:26:25.000Z",
"updatedAt": "2021-10-20T18:26:25.000Z",
"products": [
{
"name": "Açai 350ml",
// here the difference, i want create a single object in the return, with all data i need of the product
"quantity": 2,
"price": "0.000"
}
},
{
"name": "acai 350ml",
"quantity": 3,
"price": "0.000"
}
]
}
My controller
async getOrder(req, res) {
const { id } = req.params;
const order = await Orders.findByPk(id, {include: [{
association: 'products',
attributes: ['name'],
through: {
attributes:['quantity', 'price'],
},
raw: true,
}]})
if (!order) return res.status(404).send({ message: 'order ${`id`}' })
return res.json(order);
},
from How to format this association fetching sequelize
No comments:
Post a Comment