This code
var queryChain = function(done) {
var foodToSearch = "burrito";
var query = Person.find({favoriteFoods: [foodToSearch]}).sort().limit(2).select('-age');
query.exec((err,data)=>{
if(err){return done(err)};
return done(null, data);
})
};
returns “the data array length is not what expected” in the tests console. Any help?
1 Like
You need to sort it by name. Pass {name:1} in sort().
2 Likes
Yu already have the callback function as argument
function(done)
You can use the argument as callback to .exed
exec(done)
@akuokojnr -> To pass the tests, it’s important that you test to see if the foodToSearch is in the favoriteFoods array.
Answer that passes tests = Spoiler Alert
var foodToSearch = "burrito";
let findPeople = Person.find(function(error, data) {
if (data.favoriteFoods.indexOf(foodToSearch) > -1) {
return data;
}
}).sort({ name: 'asc' }).limit(2).select('-age');
findPeople.exec(function(error, data) {
error ? done(error) : done(error, data);
});
2 Likes
You can use this also
var queryChain = function(done) {
var foodToSearch = “burrito”;
Person.find({favoriteFoods: foodToSearch}, function(err, data){
if(err) done(err);
else
done(null, data);
}).sort({name: ‘asc’}).limit(2).select(’-age’).exec();
};
2 Likes
This is all you need:
var queryChain = function(done) {
var foodToSearch = "burrito";
Person.find({favoriteFoods:foodToSearch}).sort({ name: 1 }).limit(2).select('-age').exec((err,data) =>{
err ? done(err): done(null, data);
})
};
3 Likes
IBP
January 1, 2020, 3:57am
7
This works also , storing the query in a variable:
var foodToSearch = "burrito";
var findBurrito = Person.find({favoriteFoods:foodToSearch})
.sort({name: 1})
.limit(2)
.select('-age')
findBurrito.exec(function(err, data){
if(err) return console.error(err)
done(null , data);
});
};