Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!