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

107
Visualizações
Trying to return 1 field from lookup of another using Mongoose

I have the following express route and I am trying to return the value of a specific field in my user model by looking up the user email. Even running the following where I am not limiting to a specific field is generating an error:

"message": "Cast to string failed for value \"[object Object]\" at path \"email\"",

My code:

app.post('/users/forgot',(req, res) => {
var body =  _.pick(req.body, ['email']);
    User.find({
        email: body
    }).then((user) => {
        res.send({user});
    }, (e) => {
        res.status(400).send(e);
    });
});

How can I return the value I am looking for?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

As mentioned in documentation, _.pick returns the object containing the selected value. So, in the following line, body is object, not string

var body =  _.pick(req.body, ['email']); // {'email' : 'some@some.com'}

Try

app.post('/users/forgot',(req, res) => {
  var body =  req.body.email; // or  _.get(body, 'email')
  User.find({
    email: body
  }).then((user) => {
    res.send({user});
  }, (e) => {
    res.status(400).send(e);
  });
});
over 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