skip to Main Content

I have a simple node express app and I just try to connect to my MongoDB atlas. But It pulls this error. I also deleted node_modules and download all packages again but it doesn’t work.

I get this error:
Cannot read properties of undefined (reading ‘constructor’)

I just import mongoose at start of the page like this:

import mongoose from 'mongoose'

And that is my connection function:

const connectToMongo = async () => {
  try {
    await mongoose.connect(process.env.DATABASE_URL as string, { autoIndex: true, dbName: 'personal-blog' });
    console.log('connected to MongoDB');
  } catch (error) {
    console.log('error connection to MongoDB:', error.message);
  }
};

2

Answers


  1. Chosen as BEST ANSWER

    I just add this for options:

    autoIndex: true
    

    it works


  2. MongoDB may ignore fields set to undefined, but Mongoose doesn’t as it has a schema to work from and will try and cast values to the right types as defined in the schema.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search