0

I know this is simple but for some reason I can not find the solution for the life of my at all. I am trying to create an axios request and pass data into request to find a single user with a username.

I am passing in a proper username so the username is not the issue. the issue is that the data is undefined on the other side of the request which is made using sequelize.

on postman the request is working properly when I put the following iin the body of the request:

{
    "username": "burk"
}

Here is the request:

axios.get('http://localhost:9999/api/users/getUser', {
    params: {
      username: usereUsername,
    }
  })
  .then(res => {
    console.log('successfully grabbed user by username')
  }).catch(err => {
  })

this is the sequelize portion:

const getUser = (req, res) => {
  var id = req.body.username;
  console.log(id)
  User.findAll({ where: { username: id }})
  .then(users => {
    console.log(users)
    res.send(users);
  }).catch(err => {
    res.status(500).send({
      message: err.message || 'Some error occurred while retrieeing notes.'
    });
  });
}
const getUser = (req, res) => {
  var id = req.body.username;
  console.log(id)
  User.findAll({ where: { username: id }})
  .then(users => {
    console.log(users)
    res.send(users);
  }).catch(err => {
    res.status(500).send({
      message: err.message || 'Some error occurred while retrieeing notes.'
    });
  });
}

when i run request through axios, I get this :

undefined
GET /api/users/getUser?username=helm 500 19.836 ms - 74

UPDATE:

this is the postman request I am sending:

postman request

Phil
  • 157,677
  • 23
  • 242
  • 245
  • GET requests do not have a request body. Are you sure you're not executing a POST request in Postman? – Phil Aug 09 '21 at 04:57
  • I want to do an axios get request with a username that is passed through so that in sequelize, I can do a findAll request to the database and grab the specific user with the passed through username. That is what I want to do but I am not sure how to do that. @Phil – omar jandali Aug 09 '21 at 05:22
  • I will add a photo to the question. – omar jandali Aug 09 '21 at 05:23
  • Then use `req.query.username`. See https://expressjs.com/en/api.html#req.query – Phil Aug 09 '21 at 05:24
  • the issue is coming from axios. IIt is not passing in the parameters that I am sending. the issue iis not with the sequelize.. @Phil – omar jandali Aug 09 '21 at 05:25
  • The issue is Postman. If it allows you to send a request body for GET requests, then it is operating incorrectly. Axios is doing the right thing – Phil Aug 09 '21 at 05:26
  • So I would have to send the username as a query in the url?? not a body?? @Phil – omar jandali Aug 09 '21 at 05:28
  • That is exactly how you're sending it with Axios right now. On the server-side, you would need to get the value from `req.query.username`. The alternative is to use a POST request – Phil Aug 09 '21 at 05:30
  • It worked when i switched to query. Can you make it an answer so I can approve it incase someonee else reads this question. – omar jandali Aug 09 '21 at 05:33
  • For background, here is why / when Postman allowed GET requests with a body / payload ~ https://github.com/postmanlabs/postman-app-support/issues/131. I do not recommend going against standards – Phil Aug 09 '21 at 05:34

0 Answers0