Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

226
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda