const response = await OrderDal.findAll({
where: { buyerId: userId },
attributes: { exclude: ['outcomeId', 'programId'] },
include: [
{
model: User,
as: 'buyerOrders',
attributes: ['name'],
},
{
model: User,
as: 'sellerOrders',
attributes: ['name'],
},
{
model: Outcome,
attributes: ['title'],
},
{
model: ProgramOrder,
as: 'programOrders',
attributes: ['orderId', 'totalUnitsValue', 'CPO', 'PPO'],
include:
{
model: Program,
as: 'program',
attributes: ['title'],
},
},
],
nest: true,
});
This is my query of listAllOrders return all the foreign keys as the respective value and ProgramOrder table ia a manytomany association table of Program and Order and I want to enlist all the programOrder data as an array where all the programOrder has the same order_id
This is my response that I get
"OrderList": [
{
"id": 1,
"buyerId": 1,
"sellerId": 2,
"channelPartnerId": 1,
"updatedById": null,
"orderNumber": 123,
"status": "random_status",
"settlementDate": "2024-02-19T07:32:01.641Z",
"archiveDate": "2024-02-19T07:32:01.641Z",
"serviceFee": 1.4,
"total": 785,
"orderDate": "2024-02-19T07:32:01.641Z",
"invoiceDoc": "random_invoice_doc",
"noOfOutcomes": 7257,
"createdAt": "2024-02-19T07:32:01.641Z",
"updatedAt": "2024-02-19T07:32:01.641Z",
"buyerOrders": {
"name": "Numaira"
},
"sellerOrders": {
"name": "SELLER"
},
"outcome": {
"title": "Culture of Innovation"
},
"programOrders": {
"orderId": 1,
"totalUnitsValue": 200,
"CPO": 0.5,
"PPO": "0.6",
"program": {
"id": 1,
"title": "Quality Education for Refugees"
}
}
},
]
I want this type of response
"OrderList": [
{
"id": 1,
"buyerId": 1,
"sellerId": 2,
"channelPartnerId": 1,
"updatedById": null,
"orderNumber": 123,
"status": "random_status",
"settlementDate": "2024-02-19T07:32:01.641Z",
"archiveDate": "2024-02-19T07:32:01.641Z",
"serviceFee": 1.4,
"total": 785,
"orderDate": "2024-02-19T07:32:01.641Z",
"invoiceDoc": "random_invoice_doc",
"noOfOutcomes": 7257,
"createdAt": "2024-02-19T07:32:01.641Z",
"updatedAt": "2024-02-19T07:32:01.641Z",
"buyerOrders": {
"name": "Numaira"
},
"sellerOrders": {
"name": "SELLER"
},
"outcome": {
"title": "Culture of Innovation"
},
"programOrders": [
{
"orderId": 1,
"totalUnitsValue": 200,
"CPO": 0.5,
"PPO": "0.6",
"program":[
{
"id": 1,
"title": "Quality Education for Refugees"
},{
"id": 2 ,
"title": "WOMEN Education for Refugees"
},
],
{
"orderId": 1,
"totalUnitsValue": 200,
"CPO": 5.5,
"PPO": "0.9",
"program": [
{
"id": 2,
"title": "Quality Education for Refugees"
},
]
}
},
]
Just go to the models file for ProgramOrder and check if your association looks somewhat like this:
Do the same in the model file for OrderDal.
Note: In order to get an array you should have hasMany in your associations, if it is hasOne it will return as an object.