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

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

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 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