Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix 'MongoParseError: Multiple text records not allowed at ...' on MongoDB connection

it's the first times I write here and I don't speak English well, so if I'm wrong please critics in a good way.

So, the problem is that when I restart my node.js after having already started it, I get this error and I have to wait like 10/20 sec to restart and get the db connection.

ERROR:

[nodemon] starting `node server.js`
Not Connected:  { MongoParseError: Multiple text records not allowed
    at QueryReqWrap.dns.resolveTxt [as callback] (/Users/guy/Desktop/Progetto PWM/progetto/node_modules/mongodb-core/lib/uri_parser.js:93:27)
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:198:10)
  name: 'MongoParseError',
  [Symbol(mongoErrorContextSymbol)]: {} }

I am following a video tutorial that explains basics on Nodejs connection with MongoDB and these are my dependencies:

"body-parser": "^1.19.0",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"mongoose": "^5.6.7",
"morgan": "^1.9.1"

and my .env file contains:

MONGO_URI= mongodb+srv://<name>:<password>@myproject-0aqq2.mongodb.net/test?retryWrites=true&w=majority

where inside brackets there are my data.

EDIT

this is the code I used to connect

mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true})
.then(() => console.log("Connected"))
.catch(err => {
  console.log("Not Connected: ", err);
});
like image 589
Andrea Anfuso Avatar asked Oct 15 '22 12:10

Andrea Anfuso


1 Answers

The solution to this issue is to use the Standard Connection String Format for Sharded Clusters as specified in the documentation:

mongodb://<user>:<password>@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/admin

@mongos0.example.com:27017 is to be replace with the primary host name, with the 2 secondary hosts following that. You can get the addresses and ports for the 3 hosts in the Cluster Overview in the Altas Web Interface. Be sure to replace the /admin with the name of your database to which you wish to connect.

This is the recommended String Connection Format (3.4) to be used with mongoose - I checked this with the mongo support people, who were very helpful!

like image 75
coderwurst Avatar answered Oct 18 '22 01:10

coderwurst