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
Here is what I used to successfully connect to a MongoDB database. I’ve confirmed this works for both locally hosted and MongoDB Atlas
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:
If you’re still having trouble, try uncommenting the
mongoose.set( 'debug', true );
and see what your console tells you.Hope this helps!
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,
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).
3.) use .env file and define MONGO_URI like this –
and in your js file do it like this –
Try this and let me know wether it worked or not.