skip to Main Content

After removing Mongoose-package and re-installing it again I got stuck with an error.

The error I get seems to conflict with the instruction on Mongo DB Atlas instruction. where to place username and password in the dbURI.

error:

MongoParseError: credentials must be an object with ‘username’ and ‘password’ properties

This is my connection:

 const dbURI =  "mongodb+srv://admin:[email protected]/myApp?retryWrites=true&w=majority"

const options = {
    auth: { authSource: 'admin'},
    useUnifiedTopology: true,
    useNewUrlParser: true,        
}    

mongoose.set('strictQuery', false);
mongoose.connect(dbURI, options)
    .then(() => console.log('MongoDB Connected'))
    .catch(err => console.log(err))

The error indicates to place the username and password to the option object

I use node v 19.3 and Mongoose 6.8.2.

2

Answers


  1. Chosen as BEST ANSWER

    const options = {
      autoIndex: false, // Don't build indexes
      maxPoolSize: 10, // Maintain up to 10 socket connections
      serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
      socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
      family: 4 // Use IPv4, skip trying IPv6
    };
    mongoose.connect(uri, options)

    According to the official Mongoose documents these are options-object.


  2. You don’t need to specify the username and the password in the connect method of mongoose since it has already been inserted in the URL itself. In another way, there is no need to add any options when connecting to your Atlas.

    Here is an example:

     const dbURI =  "mongodb+srv://admin:[email protected]/myApp?retryWrites=true&w=majority"
    
    const options = {
        auth: { authSource: 'admin'},
        useUnifiedTopology: true,
        useNewUrlParser: true,        
    }    
    
    mongoose.set('strictQuery', false);
    mongoose.connect(dbURI, options)
        .then(() => console.log('MongoDB Connected'))
        .catch(err => console.log(err))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search