Home > Blockchain >  Mongoose doesn't connect Mongo Db Atlas
Mongoose doesn't connect Mongo Db Atlas

Time:04-08

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

CodePudding user response:

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.

CodePudding user response:

I just add this for options:

autoIndex: true

it works

  • Related