Search code examples
javascriptjsondiscord.jsembed

.toLocaleString() come as undefined


So I wrote a code that gets information from https://api.hypixel.net/ which is in JSON,

var wolfxpp = response10.data.profile.members[uuis].slayer_bosses.wolf.xp.toLocaleString()

If a user doesn't have XP .toLocaleString comes as undefined How can I fix that and make it on embed as '0'

I tried

if (response10.data.profile.members[uuis].slayer_bosses.wolf.xp == undefined) {
  return wolfxpp = '0';
}

The error stopped showing after this but the embed still haven't been sent How can I define an undefined value?


Solution

  • return wolfxpp = '0'; is not how it works

    You could do this

     wolfxpp = response10.data.profile.members[uuis].slayer_bosses.wolf.xp;
     if (wolfxpp === undefined) wolfxpp = '0';
    

    This is simpler:

    var wolfxpp = (response10.data.profile.members[uuis].slayer_bosses.wolf.xp || 0).toLocaleString()
    

    If there is no xp, use 0 instead. The (var || 0) is testing for any falsy value and returns 0 if falsy