I don’t know why my MongoDB Won’t connect. There is no errors, it just doesn’t connect. When I try Mongo Client, I download it. But then for some reason when I connect to that one it doesn’t work it takes so long the loading and then it doesn’'t work. When I try the second option it doesn’t save either. Here is some code :
const mongoose = require('mongoose');
module.exports = {
init: () => {
const dbOptions = {
useNewUrlParser: true,
useUnifiedTopology: true,
autoIndex: false,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500,
poolSize: 5,
connectTimeoutMS: 10000,
family:4
};
mongoose.connect('mongodb+srv://(here i put my username):(here i put my pass)@cluster0.duurj.mongodb.net/<dbname>?retryWrites=true&w=majority', dbOptions);
mongoose.set('useFindAndModify', false);
mongoose.Promise = global.Promise;
mongoose.connection.on('connected', () => {
console.log('Mongoose has successfully connected!');
});
mongoose.connection.on('err', err => {
console.error(`Mongoose con
nection error: \n${err.stack}`);
});
mongoose.connection.on('disconnected', () => {
console.warn('Mongoose connection lost');
});
}
}
Setup MongoDB’s firewall to allow connection from any IP address, because Glitch servers change their IP address often.
Now the username and password are in the git repository, i.e. your code rewind history. When you get this working, you should put username and password in the .env file, and then change them.
The code shown is structured as a Node module, so after doing a require, the server code can call init() to run it.
Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
at NativeConnection.Connection.openUri (c:\Users\devserver\Documents\English\node_modules\mongoose\lib\connection.js:800:32)