skip to Main Content

i’m setting up mongoDB and i keep getting error Error: Invalid schema, expected mongodb or mongodb+srv

please help me,

const mongoose = require('mongoose')


const connectionString = "mongodb + srv://PLACEHOLDER:<PLACEHOLDER>@cluster0.czgrzdd.mongodb.net/TASK-MANAGER?retryWrites=true&w=majority&appName=Cluster0"


mongoose.connect(connectionString).then(() => console.log('connected to the db')).catch((err) => console.log(err))`

the placeholders are from me not the code i don’t want to leak my password and username but i already looked for problems and couldn’t find any

I have no idea how to fix this or where to start please help me

2

Answers


  1. Here is what I used to successfully connect to a MongoDB database. I’ve confirmed this works for both locally hosted and MongoDB Atlas

    // ----
    // Connect To Database
    mongoose.Promise = global.Promise;
    
    if ( process.env.NODE_ENV !== 'test' ) {
        mongoose.connect(
            process.env.MONGO_URI, // Replace with how you wish to handle your connection string. Though, environment variable is recommended for security.
            {
                useNewUrlParser: true, 
                useUnifiedTopology: true
            }
        );
    
        mongoose.connection.once( 'open', () => {
            console.log( '✅ MongoDB Connection Successful' );
        }).on( 'error', ( error ) => {
            console.error.bind( console, `🚫 ${ error.message }` );
        });
    
        // mongoose.set( 'debug', true );
        // ^^ uncomment for debugging
    }
    

    Also, make note to remove the space between mongodb and + and put in placeholder for where your password should be. What you provided should now look like:

    const connectionString = "mongodb+srv://USERNAME:[email protected]/TASK-MANAGER?retryWrites=true&w=majority&appName=Cluster0"
    

    If you’re still having trouble, try uncommenting the mongoose.set( 'debug', true ); and see what your console tells you.

    Hope this helps!

    Login or Signup to reply.
  2. There might be multiple reasons for this error, i’ll try and the state the most obvious ones. Iam assuming you have checked whether the database with the same name exists and the username,password,cluster-name in the string are correctly mentioned and you have tried connecting database at your localhost (local computer) and it worked. If not then kindly do it.

    1.) Try using latest version of mongoose,

    npm uninstall mongoose
    npm i mongoose
    

    2.) I guess this is because of spaces present in your link. Just copy below code and paste it in your js file (make sure to replace link properly).

    const mongoose = require('mongoose');
    
    const connectionString = "mongodb+srv://PLACEHOLDER:@cluster0.czgrzdd.mongodb.net/TASK-MANAGER?retryWrites=true&w=majority&appName=Cluster0";
    
    mongoose.connect(connectionString).then(() => console.log('connected to the db')).catch((err) => console.log(err));
    

    3.) use .env file and define MONGO_URI like this –

    MONGO_URI=mongodb+srv://PLACEHOLDER:@cluster0.czgrzdd.mongodb.net/TASK-MANAGER?retryWrites=true&w=majority&appName=Cluster0
    

    and in your js file do it like this –

    const mongoose = require('mongoose')
    require('dotenv').config(); 
    
    function connectDB() {
        mongoose.connect(process.env.MONGO_URI,)
        const connection = mongoose.connection
    
        connection.once('open', () => {
            console.log('Database Connected')
        })
    
        connection.on('error', (err) => {
            console.log(`${err}`)
        })
    }
    
    module.exports = connectDB
    

    Try this and let me know wether it worked or not.

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