Passing params to the backend for API's

I am trying to return a user list on a component from my backend and I am having trouble passing my userID param back. I am using useEffect to pull the data from Redux then call an action on the component with what I think is the declared param. The correct userID is being passed correctly as far as I can see however I think the error is occuring when the route is passed the param.

In the backend controller should this be handled by req.body. userID or param.userID. I have tried multiple combination but I can’t get any of them working. My Postman get with the user number is no longer working since I added the userID instead of id so I can’t understand why it accepts id and not userID. As it is passed from the front-end as userID and I multiple other types of params that I need to call I can’t rename it on the front-end.,

component

const user = useSelector(state => state.auth.user);

    const [userDiveLog, setUserDiveLog] = useState({
        user: [],
        userDiveLogList: [],

        expanded: false
    })

    // get access to dispatch
    const dispatch = useDispatch();

    useEffect(() => {
        dispatch(fetchUserDiveLog(userDiveLog.user.userID));
    }, []);

action

// pulls the user dive log
export function fetchUserDiveLog(params, credentials, diveLogUserList){
    return function(dispatch){
        return axios.get("http://localhost:5002/api/divelog/userdiveloglist/" + params.userID)
            .then(({data}) => {
                dispatch(userDiveLogList(data));
            });
    };
}

Backend

route

//  return an individual dive log
    app.get('/api/divelog/userdiveloglist/:userID', controller.userDiveLog);

controller

exports.userDiveLog = (req, res, params, userID) => {

    try {
        const userID = req.params.userID

        diveLog.findAll({
            include: {all: true},
            where: {diverUserNumber: userID}
        })
            .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
            });
        }
};

Without putting a lot of work into, my suggestion is to log everything. That’s what I do in these (quite frequent) situations. On the frontend, log the request string you pass to axios.get() and the response. On the backend, log the request at the top of the route and the response at the end to see what the route sees and what it sends back.

I believe you should be using req.param here as that corresponds to a chunk of a route. req.body is usually in POST request bodies and req.query is the named arguments to a route. But like you, I get confused on this all the time and just go look in the express docs.

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