Hello MongoDB people,
Im trying to connect to a MongoDB hosted on Railway, but I keep getting this error:
MongoServerError: Authentication failed.
at Connection.onMessage (C:\SubMAN\Backend-2\railway_test\node_modules\mongodb\lib\cmap\connection.js:201:30)
at MessageStream.<anonymous> (C:\SubMAN\Backend-2\railway_test\node_modules\mongodb\lib\cmap\connection.js:59:60)
at MessageStream.emit (node:events:513:28)
at processIncomingData (C:\SubMAN\Backend-2\railway_test\node_modules\mongodb\lib\cmap\message_stream.js:124:16)
at MessageStream._write (C:\SubMAN\Backend-2\railway_test\node_modules\mongodb\lib\cmap\message_stream.js:33:9)
at writeOrBuffer (node:internal/streams/writable:391:12)
at _write (node:internal/streams/writable:332:10)
at MessageStream.Writable.write (node:internal/streams/writable:336:10)
at Socket.ondata (node:internal/streams/readable:754:22)
at Socket.emit (node:events:513:28) {
ok: 0,
code: 18,
codeName: 'AuthenticationFailed',
connectionGeneration: 0,
[Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}
Here is my code:
import mongoose from "mongoose";
const connect = async () => {
mongoose.connect('mongodb://mongo:<password>@containers-us-west-85.railway.app:6949')
.then(() => console.log('Connected to database movieDB'))
.catch((err: any) => console.log(err));
}
connect();
The connection uri should be right, since I was able to connect to the DB using MongoDBCompass and the built in IntelliJ Database too.
Any help would be awesome,
Thanks in advance!