I am currently developing an endpoint where I want to return one object from 4 different tables. I get the record of each table in JSON format from sequelize. I have a location, service, staff, and Tenant. How can I merge them into one object but nested. For example: The data I get from sequelize for staff:
{
"id": 2,
"profession": "Dentist",
"note": "Good Man",
"holidays": "Saturday",
"specialDays": null,
"createdAt": "2023-01-27T14:23:52.000Z",
"updatedAt": "2023-01-27T14:23:52.000Z",
}
All other data are in a similar format. But I want to merge them to return something like:
{ staff:{
"id": 2,
"profession": "Dentist",
"note": "Good Man",
"holidays": "Saturday",
"specialDays": null,
"createdAt": "2023-01-27T14:23:52.000Z",
"updatedAt": "2023-01-27T14:23:52.000Z",},
location:{
"id": 1,
"name": "Branch 1",
"address": "37 Automatic Handling",
"description": "This is our main branch",
"latitude": "564233",
"longtitude": "4441256",
"visible": true,
"createdAt": "2023-01-27T14:05:37.000Z",
"updatedAt": "2023-01-27T14:05:37.000Z",
}
}
CodePudding user response:
Just make an object yourself
// (inside of an async function)
const location = await // sequelize code to get this info
const service = await // sequelize code to get this info
const staff = await // sequelize code to get this info
const tenant= await // sequelize code to get this info
return res.json({
location,
service,
staff,
Tenant: tenant, // remove "Tenant: " if the capital was a typo or you don't care
});
CodePudding user response:
This should work
const staff = {
"id": 2,
"profession": "Dentist",
"note": "Good Man",
"holidays": "Saturday",
"specialDays": null,
"createdAt": "2023-01-27T14:23:52.000Z",
"updatedAt": "2023-01-27T14:23:52.000Z",
};
const location1 = {
"id": 1,
"name": "Branch 1",
"address": "37 Automatic Handling",
"description": "This is our main branch",
"latitude": "564233",
"longtitude": "4441256",
"visible": true,
"createdAt": "2023-01-27T14:05:37.000Z",
"updatedAt": "2023-01-27T14:05:37.000Z",
};
const mergedObject = { staff, location1 };
console.log(mergedObject);