如果这个人存在于我的表中,我的查询工作正常,但如果他/她不存在; NodeJS 会给我一个错误。
function ClientConnected(remoteClient)
{
debugLog(remoteClient.name + " connected to the server. (ID: " + remoteClient.networkId + ") Loading player data..");
sql.query('SELECT * FROM `players` WHERE `character_name` = ?', [remoteClient.name], function(err, rows)
{
if(err)
{
debugLog('[DB] Error when loading ' + remoteClient.name + "'s (" + remoteClient.networkId + ') data: ' + err.code);
} else {
players[remoteClient.networkId] = {
'a_socialid' : rows[0].account_socialid,
'i_money' : rows[0].inventory_money,
};
debugLog("Successfully loaded " + remoteClient.name + "'s (" + remoteClient.networkId + ") data..");
}
});
}
events.Add("ClientConnected", ClientConnected);
如果有人使用表中已存在的名称加入,它将加载玩家数据:
但如果不是,它会给我一个错误。 (TypeError, undefined, blabla)
最佳答案
您需要检查是否返回了一条记录。也许是这样的:
if(err)
{
debugLog('[DB] Error when loading ' + remoteClient.name + "'s (" + remoteClient.networkId + ') data: ' + err.code);
} else if (rows.length === 0) {
debugLog('Nothing returned from database');
} else {
// ...
}
据推测,err
结果只会为您提供有关实际查询的反馈,而不是它返回的数据。
关于mysql - NodeJS - MySQL 查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31712838/