- Use
Model.find()
Find all the people having a given name, using Model.find() -> [Person]
In its simplest usage, Model.find() accepts a query document (a JSON
object ) as the first argument, and returns an array of matches.
It supports an extremely wide range of search options. Check it in the docs.
Use the function argument personName as search key.
My code:
var Person = mongoose.model('Person',personSchema);
var findPeopleByName = function(personName, done) {
Person.find({"name":personName},(err,data)=>{
if(err) return done(err)
return done(null,data)
})
};
new code:
var findPeopleByName = function(personName, done) {
var query = Person.find( {name: personName})
query.exec(function (err, data) {
if(err) return done(err)
return done(null,data);
});
}
Still not working
If I console log I get “Unauthorized”.
It’s pretty much the same code I’ve used in the last challenge, I don’t understand why but it doesn’t work. I also tried looking at the documentation but it’s being done in a slightly different way using exec.
Link to the challenge:
My project on glitch
{
"_id": {
"$oid": "5be82e2d748aa95227510086"
},
"favoriteFoods": [
"pizza"
],
"name": "r@nd0mN4m3",
"age": 24,
"__v": 0
}



