Skip to content

Commit

Permalink
Fix mongoose first connection error when server is up before server-d…
Browse files Browse the repository at this point in the history
  • Loading branch information
chpmnrssll committed Jan 19, 2019
1 parent e6ceaf8 commit f9c1563
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions server/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,28 @@ var DATABASE_URL = process.env.DATABASE_URL || 'http://localhost'
mongoose.connect(`mongodb://${DATABASE_URL}/posts`, { useNewUrlParser: true });

var db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error"));

db.on('error', function (error) {
// If first connect fails because server-database is'nt up yet, try again.
// This is only needed for first connect, not for runtime reconnects.
// See: https://github.com/Automattic/mongoose/issues/5169
if (error.message && error.message.match(/failed to connect to server .* on first connect/)) {
setTimeout(function () {
mongoose.connect(`mongodb://${DATABASE_URL}/posts`, { useNewUrlParser: true }).catch(() => {
// empty catch avoids unhandled rejections
});
}, 20 * 1000);
} else {
// Some other error occurred. Log it.
console.error(new Date(), String(error));
}
});

db.once("open", function(callback){
console.log("Connection Succeeded");
});

// SERVER Setup
// SERVER Setup
app.get('/posts', (req, res) => {
Post.find({}, 'title description', function (error, posts) {
if (error) { console.error(error); }
Expand All @@ -32,7 +48,7 @@ app.get('/posts', (req, res) => {
}).sort({_id:-1})
});


// Post Endpoints
app.post('/posts', (req, res) => {
var db = req.db;
Expand Down

0 comments on commit f9c1563

Please sign in to comment.