- 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.
https://mongoosejs.com/docs/queries.html
Link to the challenge:
My project on glitch
{
"_id": {
"$oid": "5be82e2d748aa95227510086"
},
"favoriteFoods": [
"pizza"
],
"name": "r@nd0mN4m3",
"age": 24,
"__v": 0
}