I am trying to pull logs belonging to a logged in user from my database. I think I have the id set-up correctly in the route however I am not sure where I am going wrong with the controller API. In Postman it is returning the catch error message and there are definitely entries in the database belonging to the user number.
I have tried a few variations but keep getting the same error. Is this the way my API is set-up
exports.userDiveLog = (req, res) => {
const id = req.params.id;
diveLog.findAll({
where: {diverUserNumber: id}
})
.then(diveLog => {
const userDiveLogList = [];
for (i = 0; i < diveLog.length; i++) {
userDiveLogList.push(diveLog[i].dataValues);
}
if (!userDiveLogList) {
return res.status(404).send({ message: "No dive logs belonging to this user" });
}
res.status(200).send({
data: userDiveLogList
})
})
.catch(err => {
res.status(500).send({
message: "Error retrieving dive log belonging to user id= " + id
});
});
};
route
app.get('/api/divelog/userdiveloglist/:id', controller.userDiveLog);