MongoDB and Mongoose - Create a Model

Tell us what’s happening:

i dont know what is the probleme please help , you will find my code below

require('dotenv').config();
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

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

const Person = mongoose.model('Person', personSchema);

let p = (done) => {
  const person = new Person({
    name: "adnene",
    age: 22,
    favoriteFoods: ["Pizza", "kaftaji"]
  });
  person.save((error, result) => {
    if (error) return done(error);
    done(null, result);
  });
};


const createAndSavePerson = (done) => {
  done(null /*, data*/);
};

const createManyPeople = (arrayOfPeople, done) => {
  done(null /*, data*/);
};

const findPeopleByName = (personName, done) => {
  done(null /*, data*/);
};

const findOneByFood = (food, done) => {
  done(null /*, data*/);
};

const findPersonById = (personId, done) => {
  done(null /*, data*/);
};

const findEditThenSave = (personId, done) => {
  const foodToAdd = "hamburger";

  done(null /*, data*/);
};

const findAndUpdate = (personName, done) => {
  const ageToSet = 20;

  done(null /*, data*/);
};

const removeById = (personId, done) => {
  done(null /*, data*/);
};

const removeManyPeople = (done) => {
  const nameToRemove = "Mary";

  done(null /*, data*/);
};

const queryChain = (done) => {
  const foodToSearch = "burrito";

  done(null /*, data*/);
};

module.exports = mongoose.model('Person', personSchema);

/** **Well Done !!**
/* You completed these challenges, let's go celebrate !
 */

//----- **DO NOT EDIT BELOW THIS LINE** ----------------------------------

exports.PersonModel = Person;
exports.createAndSavePerson = createAndSavePerson;
exports.findPeopleByName = findPeopleByName;
exports.findOneByFood = findOneByFood;
exports.findPersonById = findPersonById;
exports.findEditThenSave = findEditThenSave;
exports.findAndUpdate = findAndUpdate;
exports.createManyPeople = createManyPeople;
exports.removeById = removeById;
exports.removeManyPeople = removeManyPeople;
exports.queryChain = queryChain;

###Your project link(s)

solution: https://3000-freecodecam-boilerplate-4bu7maidvle.ws-eu117.gitpod.io

Your browser information:

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

Challenge Information:

MongoDB and Mongoose - Create a Model

We can’t see your code.

Try removing the function and extra export you added.


I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Dashboard please help me here

I just tested your code. It passes when I remove the function and export you were not asked to create. Remove the p function and the module.exports = mongoose.model('Person', personSchema); export and try again.

1 Like

the problem persiste that why i shared with you my gitbod

You still need this.

const Person = mongoose.model("Person", personSchema);

And you still need to remove the export at line 61

module.exports = mongoose.model('Person', personSchema);

Person is already exported in the starting code.

// ...some code
const Person = mongoose.model("Person", personSchema);
// ...more code

//----- **DO NOT EDIT BELOW THIS LINE** ----------------------------------
exports.PersonModel = Person;
// more exports

Just to expand on why you must keep the variable and initial export:

If you look in server.js you can see the test it is importing and testing the expected export from myApp.js.

const Person = require("./myApp.js").PersonModel;

router.use(function (req, res, next) {
  if (req.method !== "OPTIONS" && Person.modelName !== "Person") {
    return next({ message: "Person Model is not correct" });
  }
  next();
});
1 Like

thank you very much for helping me , it 's okay now