Pulling user data using by Rest API with userID

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);

enter image description here

Hello there,

It might be more informative/helpful, if you used the err. Log it to the console or similar, and see if that helps.

Got it sorted. I hadn’t set my try catch loop correctly.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.