Use model.findOne() to Return a Single Matching Document from Your Database

var findOneByFood = function(food, done) {
  
  Person.find({favoriteFoods:food},(err,data)=>{
  //  console.log(food);
  //  console.log(data);
    if(err) return done(err)
     done(null,data)
  }); 
};

Hello, I am unable get this task(Mongodb findOne()) working. I think it is correct but the test says
"
// running test
item.name is not what expected
// tests completed
"
I even checked the logs, the returned result matches the food. It would be great if someone can help with this, have been stuck here for a long time

Edit(Solution): I should be putting data[0] instead of data, I misunderstood the response would be a single object. But, the response is an array with single object

2 Likes

I should be putting data[0] instead of data, I misunderstood the response would be a single object. But, the response is an array with single object

For this exercise, shoudn’t you use function findOne instead of find?

hi guys, I used Glitch on browser-Chrome, this is my code:

var findOneByFood = function(food, done) {
  Person.findOne({favouriteFoods: food}, function(err, data) {
    if(err) return done(err);
    return done(null, data);
  });  
};

but fcc test showed as:

// running test
Missing callback argument
// tests completed

anyone else knows the question about my code? thanks a lot!

I got the exact same code (& problem) as you @ccrubby214 . Were you able to figure it out?

Maybe they are expecting to get the person’s name back, and not the name of the food? Not sure what the ‘Missing callback argument’ is referring to???

These problems aren’t very well explained.

3 Likes

You guys misspelled or used British word.

It’s favoriteFoods not favouriteFoods.

4 Likes

@shimphillip, I did that in a previous exercise, but not on this one. Definitely using American spelling.

I copy and pasted my code and checked the preview pane with @ccrubby214 's answer and found my mistake.

wrote: favoriteFood instead of favoriteFoods :sweat_smile:

Thanks for making me re-check and glad it was only a silly spelling error!

You must use Model.findOne() instead of Model.find(). It return only one document from collection

Thanks so much, it’s really a difference I ever thought:sweat_smile:

Hi guys, this is my code in the Glitch, and I believe it is the right code,

var findOneByFood = function(food, done) {
Person.findOne({favoriteFoods: food}, function(err, data) {
console.log(food)
console.log(data)
if(err) return done(err);
return done(null, data);
});
};

but when test it in the free code camp, it showed as
// running tests
item.favoriteFoods is not what expected
// tests completed

I’m very frustrated, Pls help!!

I’m not sure where the problem is - I’d need to see the rest of the code. What you have shown works fine when I copy and paste it on to my Glitch, so the error must be somewhere else.

Very useful information