X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FGame.js;h=0949fc98712c870fae9dd7d13d549ec8b0eae3cb;hb=afd3240d89a2f6191fe9426960dc0c1667b40c77;hp=0e42292eae3a4c148fada807452aa198cc54f8a8;hpb=afc426016772300cc2812e8f96dd964bc6ae739d;p=vchess.git diff --git a/server/models/Game.js b/server/models/Game.js index 0e42292e..0949fc98 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -20,7 +20,6 @@ var db = require("../utils/database"); * message: text * played: datetime * idx: integer - * color: character */ const GameModel = @@ -71,7 +70,7 @@ const GameModel = if (!!err2) return cb(err2); query = - "SELECT squares, message, played, idx, color " + + "SELECT squares, message, played, idx " + "FROM Moves " + "WHERE gid = " + id; db.all(query, (err3,moves) => { @@ -99,19 +98,22 @@ const GameModel = "SELECT gid " + "FROM Players " + "WHERE uid " + (excluded ? "<>" : "=") + " " + uid; - db.run(query, (err,gameIds) => { + db.all(query, (err,gameIds) => { if (!!err) return cb(err); gameIds = gameIds || []; //might be empty let gameArray = []; - gameIds.forEach(gidRow => { - GameModel.getOne(gidRow["gid"], (err2,game) => { + for (let i=0; i { if (!!err2) return cb(err2); gameArray.push(game); + // Call callback function only when gameArray is complete: + if (i == gameIds.length - 1) + return cb(null, gameArray); }); - }); - return cb(null, gameArray); + } }); }); }, @@ -147,9 +149,9 @@ const GameModel = { const m = obj.move; query = - "INSERT INTO Moves (gid,squares,message,played,idx,color) VALUES " + + "INSERT INTO Moves (gid, squares, message, played, idx) VALUES " + "(" + id + ",'" + JSON.stringify(m.squares) + "','" + m.message + - "'," + m.played + "," + m.idx + ",'" + m.color + "')"; + "'," + m.played + "," + m.idx + ")"; db.run(query); } });