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'));