Chain Search Query Helpers to Narrow Search Results

Chain Search Query Helpers to Narrow Search Results
0.0 0

#1

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?


#2

You need to sort it by name. Pass {name:1} in sort().


#3

Yu already have the callback function as argument

function(done)

You can use the argument as callback to .exed

exec(done)


#4

@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);
  });