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
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:PASSWORD@cluster0.czgrzdd.mongodb.net/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!