0

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

2
  • are you trying to get the value of institute? "asdfasdf"? Commented Jul 23, 2016 at 7:06
  • Hi there, I am trying to return that entire object in that array by passing in the value of the institute "asdfasdf" Commented Jul 23, 2016 at 7:11

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0
 academic = user.academic.institute; 

this should work, though I haven't tested it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.