MongoDB and Mongoose - Create and Save a Record of a Model

I keep getting the error: Cannot read properties of undefined (reading ‘modelName’).
I have been stuck for like 2 weeks. I have even tried different names and resetting my project.

###Your project link(s)

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Mobile Safari/537.36

Challenge Information:

MongoDB and Mongoose - Create and Save a Record of a Model

We need to see the actual code. Please post a GitHub repo with your code or a Replit.

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

mongoose. set( 'useNewUrlParser' true);
mongoose.set("useFindAndModify', false);
mongoose.set('useCreateIndex', true); true);
mongoose. connect( "mongodb://127 . 0.0.1:2 7017")

const connectToDataBase = () => {  mongoose.connect("mongodb://127.0.0.1:27017")
    .then(() => {
      console.log("Connected To DB Sucessfully....")
    })
    .catch((err) => {
        console.log(err)
    })
}

module.exports = connectToDataBase;

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

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

const createAndSavePerson = function(done) {

const lucasBlue = new Person({name: "Lucas Blue", age: 19,
favoriteFoods: ["hamburger", "dark chocolate"] });

lucasBlue.save(function(err, data) {
if (err) return console.error(err);
done(null, data);
 });
 };
  1. The package name is dotenv
require('dotenv').config()
  1. You have a bunch of odd spaces in the code you posted. Don’t know what that is about.

  2. You have mongoose.connect twice. It should be at the top level not inside an exported function.

  3. You are missing all the exports at the bottom of the file. Do not change the starting code beyond what you are asked to change.

  4. Are you sure you have MongoDB set up locally correctly? You can always use MongoDB Atlas instead.


I would really prefer a GitHub repo so I can be sure I’m looking at the real code and all of it.

I have dontev spelled correctly on the replit app. Some of the spacing is just for me to read it easier. As for the exports I have them all listed on my project. I just didn’t feel like copying all that information over since it wasn’t related to the error.
As for the Mongo DB here is the link:
mongodb+srv://sumneal:@cluster0.2cgcb0l.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0

The GitHub project: boilerplate-mongomongoose - Replit

Don’t do this.

const connectToDataBase = () => {  mongoose.connect("mongodb://127.0.0.1:27017")
.then(() => {
console.log("Connected To DB Sucessfully....")
    })
    .catch((err) => {
        console.log(err)
    })
}
module.exports = connectToDataBase;

Add the mongoose.connect code at the top level and use the Replit Secrets to add the environment variable.

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

I assume you are running this locally because you can’t connect to 127.0.0.1 from Replit (it is the loopback address local to your system, i.e. localhost). If not, use MongoDB Atlas instead.


When I fix the mongoose.connect and use my own MongoDB Atlas DB your Replit code passes.

Do you have any reference videos that would explain it better? I have tried all your suggestions. I just feel like I’m doing it wrong.

Your current Replit code is passing for me.

I’m sure if you search for articles and tutorials on MongoDB/Mongoose you can find them. The freeCodeCamp YouTube channel has a bunch as well.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.