Home > Back-end >  axios get res.data = null
axios get res.data = null

Time:08-09

I've had a blockage since last night and I still don't understand, let me explain. I use React, Mongo DB, and NodeJs, and axios for my API calls I created a route to retrieve the info of a user, when I test the route with PostMan, I have the user info so everything works as it should, only when I make the api call from my front, my res.data returns "null", so I think it's coming from my api call, but I can't find what's wrong. I am attaching some screenshot, thank you in advance:

API call :

 function Post() {


    axios({
        method: "get", url: "http://localhost:4000/api/auth", credentials: true,
        
        params: {
            userId: "62f045f5253a960077a8ff3f"
        }
    })
        .then((res) => {

            console.log(res);


        })
        .catch((err) => {
            console.log(err);
        });


}

Function back getOneUser:

exports.getOneUser = (req, res, next) => {
userModel.findOne({_id: req.params.userId}).select('-password -email')
    .then(post => res.status(200).json(post))
    .catch(error => res.status(404).json({error}))

}

CodePudding user response:

In express, use req.query instead of req.params.

This post might clarify the differences between them

  • Related