NodeJS Mongodb in docker compose = ECONNREFUSED
I solve my problem:
I try to setup my connection (from node) to mongodb before the mongodb server was completely up (it take 5/6 secs for the first start).
So, i just need to retry the connection few times (3/4 times) with 1 sec before each requests from node before mongo accept the request.
var connectWithRetry = function() {
return mongoose.connect(db, function(err) {
if (err) {
console.error('Failed to connect to mongo on startup - retrying in 1 sec', err);
setTimeout(connectWithRetry, 1000);
}
});
};
connectWithRetry();