From 4b62828661a6e0fb2ece2ae4ea8a933263d674ce Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Thu, 9 Jun 2016 11:48:19 +0100 Subject: [PATCH] Extract vote collection function --- result-app/server.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/result-app/server.js b/result-app/server.js index 0675afe..bd9cfc2 100644 --- a/result-app/server.js +++ b/result-app/server.js @@ -45,17 +45,24 @@ function getVotes(client) { if (err) { console.error("Error performing query: " + err); } else { - var data = result.rows.reduce(function(obj, row) { - obj[row.vote] = row.count; - return obj; - }, {}); - io.sockets.emit("scores", JSON.stringify(data)); + var votes = collectVotesFromResult(result); + io.sockets.emit("scores", JSON.stringify(votes)); } setTimeout(function() {getVotes(client) }, 1000); }); } +function collectVotesFromResult(result) { + var votes = {a: 0, b: 0}; + + result.rows.forEach(function (row) { + votes[row.vote] = parseInt(row.count); + }); + + return votes; +} + app.use(cookieParser()); app.use(bodyParser()); app.use(methodOverride('X-HTTP-Method-Override'));