Model.findById() test case failing

Tell us what’s happening:
I am failing the test case for finding a person by id. It seems to be an issue with the test case. It works when I call it myself.

Your code so far

const findPersonById = function(personId, done) {
  Person.findById(personId, (err, doc) => done(err, doc));
};

findPersonById('5e175a1ef3ae032e72209798', (err, doc) => console.log(err, doc));

Your browser information:

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

Challenge: find person by Id

Link to the challenge:
https://www.freecodecamp.org/learn/apis-and-microservices/mongodb-and-mongoose/use-model.findbyid-to-search-your-database-by-id

Hello!

Are you using glitch, no? Could you share the edit link to your project (or a github project)?

Here is the edit link: https://glitch.com/edit/#!/join/a0a2eb28-b2cf-4124-bf4f-9c73004a6645

I think it is caused by the Model you have.

age: { type: Number, min: [1, 'Must be at least one year old'], default: 1 }

Try removing the constraints on age and just have it be type Number.

If you look in server.js where the find-by-id' route is used you will see the constructor var p = new Person({name: 'test', age: 0, favoriteFoods: ['none']});

I removed the validation and it passed. I kind of figured it might be that, but I certainly did not know to look at the server.js file. Thanks!

I can’t believe the test is setting age to 0. Setting age to 0 just doesn’t seem to make sense, and we were encouraged to experiment with validations. Making sure that age was at least 1 seemed an easy enough validation to me, especially since I was defaulting it. :stuck_out_tongue_winking_eye:

Well in all fairness I did look at the browser console first when submitting and it pointed me in the right direction. But you are right that setting the age to 0 doesn’t really make much sense. Anyway, as long as something results in a learning experience time was not wasted.

Happy coding.