0
我覺得我在這裏犯了一個明顯的錯誤。我只是想在一個對象的構造函數初始化變量:如何使用節點從mySQL初始化一個變量
var Lobby = function(io, dbConnection, lobbyName) {
this.connectedPlayers = {};
this.seatedPlayers = [];
for (var i=0;i<this.NUMBER_OF_TABLES;i++) {
this.seatedPlayers[i] = [];
for (var j=0;j<this.PLAYERS_PER_TABLE;j++) {
this.seatedPlayers[i][j] = null;
}
}
if (lobbyName == "lobbya") {
dbConnection.query("SELECT id, username, rating FROM players WHERE id < 0 order by id desc;",
function (error, results, fields) {
for (var i=0;i<results.length;i++) {
var computerPlayer = new Player(results[i]["id"],results[i]["username"],results[i]["rating"]);
this.connectedPlayers[results[i]["id"]] = computerPlayer;
this.seatedPlayers[i+4][1] = computerPlayer;
this.connectedPlayers[results[i]["id"]].setSeatedAt(i+4);
}
});
}
}
當我運行此,connectedPlayers是不確定的。 (我假設是因爲它超出了範圍,因爲我在處理結果的函數中,所以如何初始化範圍之外的變量?
是有道理的...添加當然有效。非常感謝! – a2zCribbage