DoctorMcKay / node-globaloffensive

A Node.js module to connect to and interact with the CS2 game coordinator. Mostly used to get item data.
https://www.npmjs.com/package/globaloffensive
MIT License
262 stars 61 forks source link

Crash when ranking.ranking.rank_id = NULL #50

Closed Bl4CkGuuN closed 4 years ago

Bl4CkGuuN commented 4 years ago

My bot is crashing when it wants to check a profile, who didn't got a csgo rank.

How can I prevent that?

Some of my code:

csgo.setMaxListeners(50);
csgo.requestPlayersProfile(account_id, function(ranking) {

var rankid = ranking.ranking.rank_id;
var wins = ranking.ranking.wins;
var private = ranking.player_level;

if (rankid == 0) {
      var rank = "Unranked";
} else if (rankid == 1) {
      var rank = "Silver 1";
}  else if (rankid == 2) {
      ...
} else {
      var rank = "";
}

"Crash":

Got handled GC message PlayersProfile
/root/node_modules/csgobot/standalone.js:144
                var rankid = ranking.ranking.rank_id;
                                             ^

TypeError: Cannot read property 'rank_id' of null
    at GlobalOffensive.<anonymous> (/root/node_modules/csgobot/standalone.js:144:46)
    at Object.onceWrapper (events.js:286:20)
    at GlobalOffensive.emit (events.js:198:13)
    at GlobalOffensive.handlers.(anonymous function) (/root/node_modules/globaloffensive/handlers.js:98:7)
    at SteamUser.GlobalOffensive._steam.on (/root/node_modules/globaloffensive/index.js:51:12)
    at SteamUser.emit (events.js:198:13)
    at SteamUser.<anonymous> (/root/node_modules/steam-user/components/gamecoordinator.js:82:8)
    at handlers.forEach (/root/node_modules/steam-user/components/classes/HandlerManager.js:37:12)
    at Array.forEach (<anonymous>)
    at HandlerManager.emit (/root/node_modules/steam-user/components/classes/HandlerManager.js:36:12)
Exokay commented 4 years ago

Why don't you check the response of your request? It errors, if the Response is NULL, so why don't you check if the response has Data before setting a variable?

Bl4CkGuuN commented 4 years ago

Because I have absolutely no idea what I'm doing, also I hate js :D

DoctorMcKay commented 4 years ago

ranking.ranking is null, and trying to access a property of null is an error. You need to first check whether ranking.ranking is not null.