Can't connect to mongoDB Atlas

growfiHOBBY

a year ago

I have the following code in bun with elysia

// index.ts
import './db.setup'

// db.setup.ts
mongoose.connection.on('connecting', function () {
console.log('connecting to MongoDB…')
})

mongoose.connection.on('error', err => {
if (err.name === 'MongoNetworkError') {
console.error('Network error when connecting to MongoDB:', err.message)
} else if (err.name === 'MongoParseError') {
console.error('Error parsing connection string to MongoDB:', err.message)
} else {
console.error('MongoDB connection error:', err.message)
}
})

mongoose.connection.on('connected', function () {
console.log(Mongoose default connection is open to ${MONGO_DB_URI})
})
mongoose.connection.once('open', function () {
console.log('MongoDB connection opened!')
})
mongoose.connection.on('reconnected', function () {
console.log('MongoDB reconnected!')
})
mongoose.connection.on('disconnected', function () {
console.warn('Mongoose connection disconnected. Attempting to reconnect…')
})
mongoose.connect(MONGODBURI)
export default mongoose

In local works fine, I get the following logs:

connecting to MongoDB…
Running at http://[localhost:8001](localhost:8001)
🦊 Elysia is running at localhost:8001
Mongoose default connection is open to…
MongoDB connection opened!

but in railway I only get the following logs:

connecting to MongoDB…
Running at http://[localhost:8001](localhost:8001)
🦊 Elysia is running at localhost:8001

Never appears the "MongoDB connection opened!"

5 Replies

a year ago

Are you whitelisting all IPs in atlas with 0.0.0.0/0?


growfiHOBBY

a year ago

Yes, is whitelisted


a year ago

Please add error handling and verbose logging so you are able to determine what went wrong.


growfiHOBBY

a year ago

I added

mongoose.set('debug', true)

but nothing changes in the logs

Is there anything else I should add to make it more verbose?

Attachments


a year ago

I'm sorry but that's not going to sufficiently give you the observability you need, please implement proper error handling as well.


Can't connect to mongoDB Atlas - Railway Help Station