I am trying to return a specific object in an array with mongoose. My document is as follows:
我试图用mongoose返回数组中的特定对象。我的文件如下:
{
"_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
但这不起作用,因为我收到一个错误,说user.academic.institute不是一个函数。任何帮助将不胜感激
2 个解决方案
#1
1
user.academic.institute
is an array, so you can use regular array operations to find the entry you're interested in:
user.academic.institute是一个数组,因此您可以使用常规数组操作来查找您感兴趣的条目:
var academic = user.academic.institute.filter(i => i.institute === name)
.pop();
return cb(null, academic);
#2
0
academic = user.academic.institute;
this should work, though I haven't tested it.
这应该工作,虽然我还没有测试过。
#1
1
user.academic.institute
is an array, so you can use regular array operations to find the entry you're interested in:
user.academic.institute是一个数组,因此您可以使用常规数组操作来查找您感兴趣的条目:
var academic = user.academic.institute.filter(i => i.institute === name)
.pop();
return cb(null, academic);
#2
0
academic = user.academic.institute;
this should work, though I haven't tested it.
这应该工作,虽然我还没有测试过。