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?
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);
});
});