I could not get the a nested logic to work. I need to combined data from 2 table and return it to the request. I do not want a join table as I need to return an individual record from tableA first then combine with tableB record before it is returned. Below is my simplified code
exports.get_caution_reasons = async (req, res) => {
let return_data = [];
await db.sequelize.query("SELECT TableA xxxxx",{
type: QueryTypes.SELECT
}).then(recordA => {
for (let index = 0; index < recordA.length; index++) {
return_data.push({recordA[index].xxx, recordA[index].yyy})
db.sequelize.query("SELECT TableB xxxxx WHERE zzz=recordA.zzz",{
type: QueryTypes.SELECT
}).then(recordB => {
for (let index = 0; index < recordB.length; index++) {
return_data.push({recordB[index].xxx, recordB[index].yyy})
}
})
}
res.status(200).json({data: return_data});
})
};
It only return the record for TableA only. I tried various async and await to get the recordB in there but without success. Any help is useful.