I got error while running app.js on terminal and MongoDB is providing a lot of problems

60 views Asked by At

while running node app.js i was facing this problem. I was not able to solve it

throw new MongoParseError('mongodb+srv URI cannot have port number');
^

MongoParseError: mongodb+srv URI cannot have port number
at new ConnectionString (/Users/austin/Desktop/FruitsProject/node_modules/mongodb-connection-string-url/lib/index.js:146:23)
at parseOptions (/Users/austin/Desktop/FruitsProject/node_modules/mongodb/lib/connection_string.js:201:17)
at new MongoClient (/Users/austin/Desktop/FruitsProject/node_modules/mongodb/lib/mongo_client.js:46:63)
at Object.anonymous (/Users/austin/Desktop/FruitsProject/app.js:8:16)
at Module.compile (node:internal/modules/cjs/loader:1218:14)
at Module.extensions..js (node:internal/modules/cjs/loader:1272:10)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Module.load (node:internal/modules/cjs/loader:922:12)
at Function.executeUserEntryPoint as runMain (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47`
1

There are 1 answers

0
Lin Du On

When you use DNS Seed List Connection Format (the connection string prefix with mongodb+srv), the connection string URI should not contains a port number.

For example:

DNS Seed List Connection Format:

mongodb+srv://server.example.com/

Standard Connection String Format:

mongodb://mongodb0.example.com:27017

The MongoDB nodejs driver use mongodb-connection-string-url module to handle the connection string URI.

It will validate the URI, see mongodb-js/mongodb-connection-string-url/blob/v2.6.0/src/index.ts#L201

if (this.isSRV && this.hosts.some(host => host.includes(':'))) {
  throw new MongoParseError('mongodb+srv URI cannot have port number');
}

The below code will throw the error because it contains a port number:

var ConnectionString = require("mongodb-connection-string-url").default

const cs = new ConnectionString('mongodb+srv://mongodb0.example.com:27017/')

runkit