mysql - NodeJS - MySQL 查询错误

标签 mysql node.js

如果这个人存在于我的表中,我的查询工作正常,但如果他/她不存在; 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);

如果有人使用表中已存在的名称加入,它将加载玩家数据:

enter image description here

但如果不是,它会给我一个错误。 (TypeError, undefined, blabla)

enter image description here

最佳答案

您需要检查是否返回了一条记录。也许是这样的:

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/

相关文章:

node.js - NodeJS : Making routing level variables in expressJS

php empty() 无法正常工作

javascript - 展开 div 到 "read more"

mysql - 我怎样才能让这个选择查找另一个表并找到第一个匹配项?

node.js - 如何避免错误: Cannot find module ‘express’

node.js - webSocketServer node.js如何区分客户端

javascript - 错误 : Can't set headers after they are sent in express-fileupload

Node.js Cookie 解析器停止加载页面

mysql - 无法将 csv 文件加载到 mysql 表中

mysql - 用户在一段时间内购买的平均商品数量