Can't pass the Use model.find() challenge

Tell us what’s happening:
Why is the below code not working?

Your code so far
var findPeopleByName = function(personName, done) {
Person.find({name : “Robert”}, (err, fdata) =>{
if(err){
console.log(err);
}else{
done(null, fdata);
}
})
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.

Challenge: Use model.find() to Search Your Database

Link to the challenge:

You are supposed to complete the function which is supposed to find the document with the name it takes in as the argument ‘personName’
Currently, you have made it return the documents with the name ‘Robert’
Instead of giving ‘Robert’, try giving personName .

1 Like

Thanks :heartbeat:. That worked

1 Like

one thing I don’t understand is

    var findPeopleByName = function(personName, done) {
   }.

Why that?
why not just:

Person.find({name:personName}, callback)

And what is that done doing there? what is the function of done?

And what is this?

   done(null/*, data*/);

PLEASE HELP

    var findPeopleByName = function(personName, done) {
   }.

This function is called by the test suite to test your code. All the following challenges will involve you filling in these functions for the tests to work.

When you give the data to the done() function as the second argument, it allows the test suite to access the output of the function. It gets resolved into a json, and then freeCodeCamp will check the fields of the json to check that your method is returning the correct information. If there was an error, it will log details about the error to help you understand it better.

var findByName = require('./myApp.js').findPeopleByName;
router.post('/find-all-by-name', function(req, res, next) {
  var t = setTimeout(() => { next({message: 'timeout'}) }, timeout);
  Person.create(req.body, function(err, pers) {
    if(err) { return next(err) }
    findByName(pers.name, function(err, data) {
      clearTimeout(t);
      if(err) { return next(err) }
      if(!data) {
        console.log('Missing `done()` argument');
        return next({message: 'Missing callback argument'});
      }
      res.json(data);
      Person.remove().exec();
    });
  });
});

You can see how the done() function works in server.js. You can see in the above code, that if the first argument (error) exists, it logs information about it and ends in there. So we give null as the first argument so that the if(err) part doesn’t get executed, and it runs the next part where it looks at the data.

Tbh though you don’t need to know much about it, other than that it is for marking your code. Just fill in the functions so that they achieve what is intended.

1 Like

Oh! so the done() function is not naturally part of my code. It is there to test the output of my code.

Thanks Man. :heartbeat:
just get to understand these now

1 Like