Home > Net >  Failed to connect to MongoDB ( remote server )
Failed to connect to MongoDB ( remote server )

Time:06-03

I am trying to deploy an Angular/NodeJS app on the server I just bought. ( http://o2switch.fr is the provider ) I managed to deploy the angular part, but I have trouble with the connexion to mongoDB

When I launch app.js in my terminal, it says :

(node:30382) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cluster0-shard-00-00.cgjma.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 15.237.158.132:27017]
    at Pool.<anonymous> (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/server.js:441:11)
    at Pool.emit (events.js:198:13)
    at createConnection (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:564:14)
    at connect (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:1014:9)
    at makeConnection (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:32:7)
    at callback (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:300:5)
    at TLSSocket.err (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:330:7)
    at Object.onceWrapper (events.js:286:20)
    at TLSSocket.emit (events.js:198:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:30382) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:30382) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Basically, it doesn't manage to connect to MongoDB even though in localhost, it works fine.

app.js :

mongoose.connect(config.DB_URI, function(err, db){
    if (err) throw err;
    console.log('Connected to database');
}, { useNewUrlParser: true });

config/server.js :

module.exports = {
    DB_URI : 'mongodb srv://pseudo:[email protected]/rank'
} 

CodePudding user response:

Mongo connection fails. So you should handle the promise rejection. You may use .catch() method to do that. You can check this link.

  • Related