Getting error on mongoDB connection
Am getting error when connecting to mongoDB hosted on railway.app
when using the connection uri without database name i.e. mongodb://user:[email protected]:24727
it works fine and connects
but on using uri with db name
mongodb://user:[email protected]:24727/dbname
gives authentication error
MongoServerError: Authentication failed.
at Connection.onMessage (/app/node_modules/mongodb/lib/cmap/connection.js:205:26)
at MessageStream.<anonymous> (/app/node_modules/mongodb/lib/cmap/connection.js:64:60)
at MessageStream.emit (node:events:517:28)
at processIncomingData (/app/node_modules/mongodb/lib/cmap/message_stream.js:117:16)
at MessageStream._write (/app/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
at writeOrBuffer (node:internal/streams/writable:392:12)
at _write (node:internal/streams/writable:333:10)
at Writable.write (node:internal/streams/writable:337:10)
at Socket.ondata (node:internal/streams/readable:777:22)
at Socket.emit (node:events:517:28) {
ok: 0,
code: 18,
codeName: 'AuthenticationFailed',
connectionGeneration: 1,
[Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}
12 Replies
Project ID:
47c310df-3a23-400f-aa2d-abb66fa666cc
47c310df-3a23-400f-aa2d-abb66fa666cc
works fine without using database name in the url
but gives authentication error when including database name
have already checked connection and database name, collections ets in mongodb compass
if it's anything like mongose you need to specify the desired database via some config object
could you please give a reference or example
because wherever i search even when connecting mongodb atlas database name is included in in connection string @Brody
please link me to the npm page for the mongodb client you are using in code and I will look through it's documentation for you
@Brody i am using mongoose. I am surprised cause i am connecting fine with mongodb atlas with the given format
<connectionuri>/<dbname>
npm
mongoose
Mongoose MongoDB ODM. Latest version: 8.1.3, last published: 2 days ago. Start using mongoose in your project by running
npm i mongoose
. There are 15180 other projects in the npm registry using mongoose.atlas does not run the same type of mongo database as railway
oh that is mongose
any solutions?
Solution
specify the
dbName
thanks a lot @Brody .Really
no problem!