MongooseDB Not Working

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');
        });
    }
}

Please help. I would be pleased to describe.

Could you link the Mongoose docs in your post? You could try checking if your password is url encoded.
Also, what does init: do?

Could you try replacing "<dbname>" with something like "dbname"? It might not like the brackets.

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.

1 Like

I did that, but It still doesn’t work @mishavee

Do I have to incode my password in UTF - 8 Formatting?

Yeah, like if you have an exclamation mark it needs to be %21 or something.

Here’s an example, with a variable from the .env file

encodeURIComponent(process.env.MONGODB_PASS)

if a value in the .env file has special characters from the bash shell point of view, it will need to be delimited with a quote.

Is there an error message in the server log?

Wdym? It doesn’t connect to the DB my code

in the client.connect function, add
if (err) {return console.log(err)}

@mishavee

@CarlyRaeJepsenStan

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)

i did whitelist tho

If you’re running from home you can whitelist your home IP.

Did you get the connection string from MongoDB setup page? Choose the Nodejs driver …

If you are running on Glitch, you have to whitelist 0.0.0.0 as Glitch projects change IPs every 12 hours.

2 Likes