Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda