const getCompanyShifts = (req, res) => {
try {
const { company_id } = req.params;
connection.query(
`SELECT * FROM jobs WHERE company_fk=${company_id}`,
(err, rowss) => {
if (!err) {
connection.query(
`SELECT * FROM shift WHERE isBooked=1 AND fk_job = ?`,
[rowss.jobsID],
(err, rows) => {
if (err || rows.length === 0) {
res.status(404).json({
success: false,
message: "Company Shifts Not Found!",
err,
});
} else {
const shifts = [];
rows.forEach((row, i) => {
const shift = {
shiftID: rows[i].shiftID,
shiftStartTime: rows[i].startTime,
shiftEndTime: rows[i].endTime,
shiftDate: rows[i].date,
isBooked: rows[i].isBooked,
fk_job: rows[i].fk_job,
fk_guard: rows[i].fk_guard,
};
shifts.push(shift);
});
res.status(200).json({
success: true,
message: "Successfully Retrieved Company Shifts!",
shifts,
});
}
}
);
} else {
res.status(404).json({
success: false,
message: "Company Jobs Not Found!",
});
}
}
);
} catch (error) {
res.status(500).json({
success: false,
message: error.message,
});
}
};
in the first query of the above code, i am getting all the rows from the jobs table. In the second nested query, i am trying to get all rows from the shift table for each of the jobsID returned from the 1st query. But i don't get any data back. The data exists and it should return data but i don't get any data back. What am i doing wrong here ? Please help!