Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Mongodb FindOne returns object with undefined property

I'm trying to get a user from my collection with findOne using its name attribute but it returns undefined whereas my user is in the db.

This is my schema :

var schema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, "Please enter a name"],
    maxlength: 32,
  },
  password: {
    type: String,
    required: true,
    minlength: [6, "Password must be at least 6 characters"],
  },
  email: {
    type: String,
    required: true,
    unique: true,
  },
  isAdmin: {
    type: Boolean,
    default: false,
  },
});

const Userdb = mongoose.model("userdb", schema);

module.exports = Userdb;

And this is where i try to get my user :

const name = req.body.name;
  const password = req.body.password;
  const user = Userdb.findOne({name: name});
  // console.log(user.name, user.password);
  if (user.name === undefined)
    return res.status(400).send({ message: "User not found" });

  const dbpassword = user.password;

where my console.log returns undefined

In the db :

screenshot

My postman post request :

screenshot postman

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Check your body request and sure you has data in db collection with this condition. If data of collection has not match query mongoose return you undifined.

  • Send request in raw mode and set Json type to send request

Like this

And you should create async function and use await for get data then log that

const user = await Userdb.findOne({name: name});
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!