MongoDB and Mongoose - Chain Search Query Helpers to Narrow Search Results: Test fails and there is no official solution

Tell us what’s happening:
The test fails with the following code:

var queryChain = function(done) {
  var foodToSearch = "burrito";

  Person.find({ favoriteFoods: ["burrito"] })
  .sort({ name: -1 })
  .limit(2)
  .select({ age:0 })
  .exec(function(error, people) {
    //do something here
    done(null, people);
  });

  done(null, people);
};

Your code so far
https://boilerplate-mongomongoose-1.controlunit.repl.co

Your browser information:

Chromium

Challenge: Chain Search Query Helpers to Narrow Search Results

Link to the challenge:

I have a question about your topic title: what is the “official solution” that you are referring to?

The solution you provided has at least two errors that I’m noticing:

1.) Calling done function twice.
2.) Model.find may have a problem with the parameter provided.

I’d recommend reviewing Model.find in the documentation to see what parameters are allowed and fixing what you need to there.

I hope this helps!

I have edited the code according to your suggestions as follows, however, the test still fails:

var queryChain = function(done) {
  var foodToSearch = "burrito";

  Person.find({ favoriteFoods: foodToSearch })
  .sort({ name: -1 })
  .limit(2)
  .select({ age:0 })
  .exec(function(error, people) {
    //do something here
    done(null, people);
  });

  
};

There is indeed another error. Check each step carefully, each function that you’re using. Check all of them to make sure that you’re following each direction on these:

  • Find
  • Sort
  • Limit
  • Select
  • Exec

There is a problem with one of these that you need to solve.

Solution:

var personSchema = new mongoose.Schema({
  name: String,
  age: Number,
  favoriteFoods: [String]
});

var Person = mongoose.model('Person', personSchema);

var queryChain = function(done) {
  var foodToSearch = "burrito";

  Person.find({ favoriteFoods: foodToSearch })
  .sort({ name: 1 })
  .limit(2)
  .select({ age: 0 })
  .exec((err, data) => {
     if(err)
       done(err);
    done(null, data);
  })
};