Model.find() - MongoDB and Mongoose

var findPeopleByName = function(personName, done) {
    Person.find(personName, function (err, data) {
    if(err){
      return done(err);
    }
    return done(null, data);
    })
};

Why does this not work? The test console returns that “Parameter “filter” to find() must be an object, got r@nd0mN4m3”

3 Likes

Have you figured it out? If not, I can help. I ran into the same problem, but then realized that personName isn’t an object, so I turned it into an object and it worked.

No. I have not figured it out. How do I convert the parameter to an object? I tried using JSON.parse(personName) but it does not pass the test.

I found the answer. {name: personName} worked

3 Likes

Thank you very much for your help.

Could you paste your working code? I’m having trouble understanding where to put {name: personName}.
Thanks :slight_smile:

You gotta replace personName parameter into an actual JSON with key : value pair like the author did.

I’m still confused on what to do with the {name: personName}. I tried putting it in my package.json file with no luck. Can someone please send their code and how they got the answer. Thank you!

Never mind, I figured it out.

The doc for find query

var findPeopleByName = function(personName, done) {

Person.find({ name: personName }, function (err, data) {
  if (err) {
    done(err);
  }
done(null, data);
});  

//done(null/, data/);

};

Thanks for sharing this document.
It’s pretty clear now.