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

161
Visualizações
How to save a query from model.findOne() on a variable using mongoose

I am trying to save the results from model.findOne() on a variable for using the results on another function. But what I get on the variable is only a pending promise:

let key = Product.findOne({ name: 'keyboard' }).then(data => { return data });
console.log(key);

and what I see on mongo shell:

Promise { <pending> }
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

key is not the result of findOne(), it is a promise. The promise has not resolved.

To make your example work, change the code:

let key = await Product.findOne({ name: 'keyboard' }).exec();
about 4 years ago · Santiago Trujillo 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