freeCodeCamp: Use model.findOne() to Return a Single Document fails

Hi, MongoDB community. You folks must be aware of freeCodeCamp. They have a track known as Back End Development and APIs. Inside that track, there is a module for MongoDB and Mongoose.

Before I go into describing the problem, I would like to put in some context first.

Now let’s come to my problem. There are almost 12 exercises in that module and I’m stuck at 5th one. The problem statement is:

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

This is the minimal code I am using to pass this test.

require('dotenv').config();
const mongoose = require("mongoose")

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

let personSchema = new mongoose.Schema({
  name: String,
  age: Number,
  favoriteFoods: [String]
});

/** 3) Create and Save a Person */
let Person = mongoose.model('Person', personSchema);

/** 6) Use `Model.findOne()` */
let findOneByFood = function (food, done) {
  Person.findOne({ favoriteFoods: food }, function (err, data) {
    if (err) return console.log(err);
    done(null, data);
  });
};

Somehow I am failing the test for this exercise.

If you have passed this exercise, please enlighten me on what’s wrong with my code.

This might be the issue.

Hi Santosh,

Just wondering if you ever found a solution. I’m having the exact same issue ^^’

Cheers,
Alex