So I've searched around and found out that to fix the said issue, I have to return after sending a response. But my problem is, even though I have return, I still have the error.
const dbEditCourse = (req, res, db, logger) => {
let {
origCourse, code, description, type
} = req.body;
if (!code || !description || !type) {
res.json({
haveEmpty: true
});
return;
}
db.transaction((trx) => {
db.select('*').from('course_strand').where('code', '=', code)
.then(data => {
if (data[0]) {
//error happens in this block of code
res.json({
isSuccess: false
});
return;
//i also tried return res.json({ isSuccess: false });
}
//wrapping this in 'else' also does not work
return db('course_strand')
.returning('*')
.where('code', '=', origCourse)
.update({ code, description, type })
})
.then(course => {
return db('activity_logs')
.returning('*')
.insert({
date: new Date(),
employee_id: req.session.emp_id,
module: "COURSE / STRAND",
activity: "EDIT"
})
})
.then(activity => {
if (activity[0]) {
res.json({
isSuccess: true
});
return;
} else {
res.json({
isSuccess: false
});
return;
}
})
.then(trx.commit)
.catch(err => {
logger.error(err);
trx.rollback;
res.render('pages/error-500');
});
})
.catch(err => logger.error(err));
}
module.exports = {
dbEditCourse
}
What I'm doing to produce the error is, If the record is existing, it will go into the block of code above. Aside from that specific block of code, I don't encounter the error elsewhere. And the code is working fine even though I have the error.