From 0776551aa7ff4d1355c0e2b1e41906a6fb663c44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Fri, 19 Dec 2014 14:34:47 +0100 Subject: [PATCH] Fix #26 API redirects to the response before it's written to disk --- lib/server/controllers/apiController.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/server/controllers/apiController.js b/lib/server/controllers/apiController.js index 0f976d7..d76d458 100644 --- a/lib/server/controllers/apiController.js +++ b/lib/server/controllers/apiController.js @@ -52,16 +52,25 @@ var ApiController = function(app) { .then(function(data) { debug('Success'); - runsDatastore.markAsComplete(run.runId); + // Save result in datastore data.runId = run.runId; - resultsDatastore.saveResult(data); + resultsDatastore.saveResult(data) + .then(function() { - // Send result if the user was waiting - if (run.params.waitForResponse) { - res.redirect(302, '/api/results/' + run.runId); - } + runsDatastore.markAsComplete(run.runId); + + // Send result if the user was waiting + if (run.params.waitForResponse) { + res.redirect(302, '/api/results/' + run.runId); + } + + }) + .fail(function(err) { + debug('Saving results to resultsDatastore failed:'); + debug(err); + }); }).fail(function(err) {