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

166
Visualizações
Why am I getting an "undefined" result from my MongoDBQuery in Node?

I've already stablished a DB with it's schema like this:
enter image description here

When I use my search function in Nodejs it works perfectly and returns my info like this:\

enter image description here

So, why is is than when I try to fectch my user from mongodb and get the parameter "examenes":
enter image description here

I get this undefined return?:
enter image description here

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I think you should use the await keyword

try{
   var user = await model.findOne({ _id: req.params.id});
} catch (e) {
   // Handle Errors
};

Or

model.findOne({ _id: req.params.id}).then(result => {
  var user = result;
}).catch(e=> {
// Handle Errors
});
about 4 years ago · Juan Pablo Isaza Relatório

0

Have you added mergeParams in your router? This options adds params object to the request const router = express.Router({ mergeParams: true });

then /predict/:id results in req.params.id

By default this option is disabled in express

about 4 years ago · Juan Pablo Isaza Relatório

0

The problem is you are not using await so in the line console.log(user.examenes) the variable user is not the data returned by mongo; is another object without attribute examenes. And trying to access the value is undefined.

So you can use:

let user = await (usuario.findOne({_id: req.params.id}))
about 4 years ago · Juan Pablo Isaza 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