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

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

#1
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

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


#3

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


#4

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!


#5

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.


#6

You guys misspelled or used British word.

It’s favoriteFoods not favouriteFoods.


#7

@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!


#8

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


#9

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