From 7ea08f30742773abb3a9da1f8f74ea50c3946d27 Mon Sep 17 00:00:00 2001 From: Nathan Leniz Date: Wed, 28 Jan 2015 02:22:12 -0500 Subject: [PATCH] Add callback to and settimeout functions to prevent race conditions with write --- app.js | 26 ++++++++++++++++++++------ controllers/bonfire.js | 1 - public/js/main.js | 11 ++++++----- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app.js b/app.js index e3411874e6..abfa3e5685 100644 --- a/app.js +++ b/app.js @@ -134,7 +134,6 @@ var trusted = [ '*.youtube.com', ]; -debug(trusted); app.use(helmet.contentSecurityPolicy({ defaultSrc: trusted, scriptSrc: ['*.optimizely.com', '*.aspnetcdn.com'].concat(trusted), @@ -323,9 +322,17 @@ app.post('/completed-bonfire/', function (req, res) { solution: isSolution }) - req.user.save(); - pairedWith.save(); - res.redirect('/bonfires'); + req.user.save(function(err, user) { + pairedWith.save(function(err, paired) { + if (err) { + throw err; + } + if (user && paired) { + res.redirect('/bonfires'); + } + }) + }); + } }) } else { @@ -341,8 +348,15 @@ app.post('/completed-bonfire/', function (req, res) { if (index > -1) { req.user.uncompletedBonfires.splice(index,1) } - req.user.save(); - res.redirect('/bonfires'); + req.user.save(function(err, user) { + if (err) { + throw err; + } + if (user) { + res.redirect('/bonfires'); + } + }); + } }); diff --git a/controllers/bonfire.js b/controllers/bonfire.js index 1626452119..5025e68cf7 100644 --- a/controllers/bonfire.js +++ b/controllers/bonfire.js @@ -95,7 +95,6 @@ exports.returnIndividualBonfire = function(req, res, next) { next(err); } if (bonfire.length < 1) { - debug('Full Bonfire', bonfire); req.flash('errors', { msg: "404: We couldn't find a bonfire with that name. Please double check the name." }); diff --git a/public/js/main.js b/public/js/main.js index 99e2ff1c03..b8f32d468a 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -51,10 +51,6 @@ $(document).ready(function() { url: '/completed-bonfire/' }); - - //$.post( '/completed-bonfire', function( data ) { - // window.location = '/bonfires'; - //}); } } @@ -71,13 +67,18 @@ $(document).ready(function() { window.location = '/challenges/' + (parseInt(l[l.length - 1]) + 1); }); + $('.next-bonfire-button').on('click', function() { var bonfireSolution = myCodeMirror.getValue(); var thisBonfireHash = passedBonfireHash || null; var didCompleteWith = $('#completed-with').val() || null; completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash); - window.location = '/bonfires'; + + window.setTimeout(function() { + // TODO + window.location = '/bonfires'; + }, 100); });