You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ok. I fixed my problem by reinitializing the mongojs object when connection fails:
vardb=null;functionconnectToMongoDb(callback){db=mongojs(mongojsConnectionString);// key to success!db.on('error',(err)=>{});db.users.find({},(err)=>{if(err){console.log('Error connecting to db:',err.message);setTimeout(()=>{connectToMongoDb(callback);},2000);}else{callback();}});}connectToMongoDb(()=>{console.log('Connected! Whoohoo!');});
When I start my node.js app WITHOUT MongoDB up and do a
db.find
I getI then try the
db.find
every 2 seconds, but even when I start up my MongoDB afterwards, I still getWhen I restart my node.js script all works perfect.
I want this to work as there might be a case when MongoDB is not up when I start my Node.js app. Eg when using Docker compose.
The text was updated successfully, but these errors were encountered: