Ruham
Ruham

Reputation: 769

Postman returning "null" for Mongoose query

I have a Mongoose User model that contains the field username. I have created a user with the username bob97555x005

In users router, I have the following route:

router.get("/check", (req, res, next) => {

  const query = String(req.body.username).toLowerCase();

  return User.findOne({ username: query })
    .then(users => res.json(users))
    .catch(err => {
      console.log("no user found!");
    });
});

In Postman, I am hitting this route with:

http://localhost:5000/api/users/check?username=bob97555x005

This returns null. The expected behavior is returning the user json, which is in the database. Why am I getting the null?

P.S. There's nothing wrong with the router file setup itself, since I'm using it to create users.

Upvotes: 0

Views: 149

Answers (1)

Shanil Arjuna
Shanil Arjuna

Reputation: 1135

Since your using Http get method you should retrieve the passed parameter as req.query.username

const query = String(req.query.username).toLowerCase();

Edit

req.body is used when using the Http Post Method.

Upvotes: 1

Related Questions