Closed vincepunkrock closed 2 years ago
Turns out my connection string wasn't set by dotenv yet when calling mongoose.connect. When logging it in the error callback, it was set, therefore the confusion. In the end it was a problem with the way dotenv works with imports.
Bug
What is the current behavior? Here's my connection string with the values obfuscated: mongodb+srv://[user]:[password]@[host].mongodb.net/[db name]?retryWrites=true&w=majority"
When I call mongoose.connect([connectionString])...
I get this error {"name":"MongoParseError","message":"Invalid connection string","stack":"MongoParseError: Invalid connection string\n at parseConnectionString (.../node_modules/mongodb/lib/core/uri_parser.js:585:21)\n at connect (.../node_modules/mongodb/lib/operations/connect.js:283:3)\n at cb (.../node_modules/mongodb/lib/mongo_client.js:284:5)\n at maybePromise (.../node_modules/mongodb/lib/utils.js:692:3)\n at MongoClient.connect (.../node_modules/mongodb/lib/mongo_client.js:280:10)\n at Promise (.../node_modules/mongoose/lib/connection.js:836:12)\n at new Promise ()\n at NativeConnection.Connection.openUri (.../node_modules/mongoose/lib/connection.js:832:19)\n at _mongoose._promiseOrCallback.cb (.../node_modules/mongoose/lib/index.js:351:10)\n at promiseOrCallback (.../node_modules/mongoose/lib/helpers/promiseOrCallback.js:10:12)\n at Mongoose._promiseOrCallback (.../node_modules/mongoose/lib/index.js:1149:10)\n at Mongoose.connect (.../node_modules/mongoose/lib/index.js:350:20)\n at Object. ...
I looked at the 'parseConnectionString' code, it uses a regexp to validate the connection string. I tested my connection string against this regexp and it does match. I don't understand what's happening. I must be missing something.
I'm using typescript so here's my tsconfig.json: { "compilerOptions": { "target": "es2016",
"module": "commonjs",
"resolveJsonModule": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true, "strict": true,
"skipLibCheck": true
} }
What is the expected behavior? When I connect to a local db for testing, with this connection string: mongodb://localhost:27017/[db name], it works. I would expect my connection string to mongodb atlas to work as well.
Nodejs: 10.16.0 Mongoose: 5.13.13