Couldn't connect to mongodb on mongodb-atlas

点点圈 提交于 2019-12-13 03:19:35

问题


Trying to connect to the mongodb free database on mongodb-atlas I've almost tried everything Checked the docs, Changed the url, Changing password and even deleted the user and created the new one but still not resolve the problem

here is myurl.js file

module.exports = {
    mongoURL: "mongodb+srv://nansDB:nansDB123@nodecluster-qs6cv.mongodb.net/test?retryWrites=true&w=majority",
    secret: "mystrongsecret"
}

here is my index.js file

const express = require('express');
const mongoose = require('mongoose');
const app = express();

//mongodb Configuration
const db = require('./setup/myurl').mongoURL;

//attempt to connect to database
mongoose.connect(db,  { useNewUrlParser: true })
        .then(()=> console.log('MongoDb Connect successfully'))
        .catch(err => console.log(err));

const port = process.env.PORT || 3000;

here are the error lines I'm getting whenever I tried to run

{ MongoNetworkError: failed to connect to server [nodecluster-shard-00-02-qs6cv.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to nodecluster-shard-00-02-qs6cv.mongodb.net:27017 closed]
    at Pool.<anonymous> (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\topologies\server.js:431:11)
    at Pool.emit (events.js:189:13)
    at connect (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\pool.js:557:14)
    at callback (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\connect.js:109:5)
    at runCommand (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\connect.js:129:7)
    at Connection.errorHandler (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\connect.js:321:5)
    at Object.onceWrapper (events.js:277:13)
    at Connection.emit (events.js:189:13)
    at TLSSocket.<anonymous> (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\connection.js:350:12)
    at Object.onceWrapper (events.js:277:13)
    at TLSSocket.emit (events.js:189:13)
    at _handle.close (net.js:597:12)
    at TCP.done (_tls_wrap.js:388:7)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

回答1:


Its solved actually my whitelisted ip was changed because of connection issue [reconnecting to wifi]. Although my suggestion is checked whether the ip you whitelisted is exists or not.



来源:https://stackoverflow.com/questions/56642608/couldnt-connect-to-mongodb-on-mongodb-atlas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!