Tell us what’s happening:
I’m working on this exercise where we need to complete a function using findOne mongoose method.
For context, the schema for our model is:
{
name : {
type: String,
required: true
},
age : Number,
favoriteFoods : {
type: Array,
default: []
}
}
In the exercice, we must write a function, with a food
parameter, and that returns the first item in DB that has this food
argument as part of the favoriteFoods array.
I searched and struggled. Eventually checked out the solution below
Person.findOne({favoriteFoods: food}, function (err, data) {
if (err) return console.log(err);
done(null, data);
});
};
And now I don’t get how this can work since favoriteFoods
is an array and food
isn’t. Is that under the hood dark mongoose magic or am I missing something?
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36
Challenge: Use model.findOne() to Return a Single Matching Document from Your Database
Link to the challenge: