Skip to content

Commit 4b62828

Browse files
aanandbfirsh
authored andcommitted
Extract vote collection function
1 parent 1f1a6f9 commit 4b62828

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

result-app/server.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,24 @@ function getVotes(client) {
4545
if (err) {
4646
console.error("Error performing query: " + err);
4747
} else {
48-
var data = result.rows.reduce(function(obj, row) {
49-
obj[row.vote] = row.count;
50-
return obj;
51-
}, {});
52-
io.sockets.emit("scores", JSON.stringify(data));
48+
var votes = collectVotesFromResult(result);
49+
io.sockets.emit("scores", JSON.stringify(votes));
5350
}
5451

5552
setTimeout(function() {getVotes(client) }, 1000);
5653
});
5754
}
5855

56+
function collectVotesFromResult(result) {
57+
var votes = {a: 0, b: 0};
58+
59+
result.rows.forEach(function (row) {
60+
votes[row.vote] = parseInt(row.count);
61+
});
62+
63+
return votes;
64+
}
65+
5966
app.use(cookieParser());
6067
app.use(bodyParser());
6168
app.use(methodOverride('X-HTTP-Method-Override'));

0 commit comments

Comments
 (0)