super
October 2, 2020, 1:14am
1
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:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
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!
super
October 2, 2020, 5:32am
3
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.
super
October 4, 2020, 6:37pm
5
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);
})
};