Reputation: 1310
I am trying to return a specific object in an array with mongoose. My document is as follows:
{
"_id": {
"$oid": "577a9345ba1e2a1100624be7"
},
"name": "John Doe",
"password": "$2a$10$NzqAqxTRy8XLCHG8h3Q7IOLBSFCfBJ7R5JqHy1XHHYN.1h074bWJK",
"__v": 0,
"birthDate": "14.07.2016",
"academic": [
{
"about": "asdfasdf",
"to": "asdf",
"from": "asfdasdf",
"institute": "asdfasdf",
"qualification": "asdfasdf",
"_id": {
"$oid": "579111b3e68d489f1ff8b6dc"
}
}
]
}
I want to return that academic object in the list. I am passing in the institute name into the route my code is as follows:
getAcademicInstituteByName: function(req, name, cb){
User.findById(req.user.id, function (err, user) {
if(err) throw err;
if(user){
academic = user.academic.institute(name);
return cb(null, academic);
}
});
But this is not working since I am getting an error saying user.academic.institute is not a function. Any help would be greatly appreciated
Upvotes: 0
Views: 178
Reputation: 203231
user.academic.institute
is an array, so you can use regular array operations to find the entry you're interested in:
var academic = user.academic.institute.filter(i => i.institute === name)
.pop();
return cb(null, academic);
Upvotes: 1
Reputation: 780
academic = user.academic.institute;
this should work, though I haven't tested it.
Upvotes: 0