MongoDB and Mongoose - Create a Model

Tell us what’s happening:

I have created the schema and model on myApp.js file. Is it the right file to put create schema and model?
The instructions also point me to use done function, but I am confused in where to use it

This is my code on myApp.js

require('dotenv').config();
var mongoose = require('mongoose');


let Person;

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*/);
};

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

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

Person = 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: http://localhost:3000

Your browser information:

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

Challenge Information:

MongoDB and Mongoose - Create a Model

Hi there! :waving_hand:
You’re almost there, just a couple of quick fixes:

  1. You should declare your Person model using const
  2. The favoriteFoods field should be an array of strings, not a single string.

These changes will help your schema meet the expected structure. Good luck!

Yes, App.js is the correct file to add your code to, but double check the type for favoriteFoods.

Please think about where this code should be placed in App.js since you need to connect to the database, set up the schema and declare Person before any of the functions can be implemented.

You can declare and initialize Person on one line.

Also, isn’t there a better choice than var here?

Thank you for the tips!

Thank you for the point out!

1 Like