MongoDb and Mongoose - Create a model

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

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

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

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


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

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

  1. Here is all my code, with this code ‘npm start’ and then I just copy and paste the URL of localhost, but challenge is not passed. What is wrong with this code? or anything I have to check?

  2. A message in terminal:
    (node:42528) [DEP0170] DeprecationWarning: The Url mongodb://~~~~~~~~~is invalid.Future versions of Node.js will throw an error.
    (Use node --trace-deprecation ... to show where the warning was created)

  3. A message in challenge:
    x: Creating an instance from a mongoose schema should succeed

please help me~

I couldn’t get the code to pass either, so I took help of chatgpt.
I figured it was my .env file.

My initial mongo_uri: (default MongoDB format URI)

mongodb+srv://<db_username>:<db_password>@cluster0.6ohpp.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0

Corrected:

MONGO_URI=mongodb://yourUsername:yourPassword@localhost:27017/yourDatabase

This is all that I changed.
maybe the error is in .env for you too?

I even destructured the Schema variable:

const { Schema } = mongoose;

is equivalent to

const Schema = mongoose.Schema;

TYPO.

favoriteFoods: [String]

Hi @sharky606 and welcome to the freeCodeCamp forum!

I see you have been responding to an old thread with a similar problem than yours. Thanks for the contributions though! They are to the point.

If you are having an issue where we can also help you and that doesn’t relates to the original post, please let us know by opening a new post so we can better identify the problem?

Again, thanks for taking your time trying to help others and happy coding!