1

My query works fine if the person exist in my table, but if he/she does not; the NodeJS will give me an error.

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);

If someone joins with the name that already exists in the table, it will load the players data:

enter image description here

But if not, it gives me an error. (TypeError, undefined, blabla)

enter image description here

0

1 Answer 1

1

You need to check that a record was returned. Maybe something like this:

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 {
    // ...
}

Presumably the err result would only give you feedback on the actual query, not the data it returns.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.