0

So I'm new to Javascript and trying to making a discord bot. Here is a very small portion that illustrates my problem:

    module.exports = {
    name: "match",
    category: "LOL",
    description: "Give Summoner's Data to User",
    run: async (client, message, args) => {
        var username = `${args}`
        var regionID= "na1"

        pyke.summoner.getBySummonerName(username, regionID).then(data => {
            return pyke.spectator.getCurrentGameInfoBySummoner(`${data.id}`, "na1").then(result => {
                try {
                    console.log(result)
                } catch (err) {
                    console.error(`${args} isn't in game!`)
                }
            })
    })
    }
}

I was expected to see if an error cause, it will send a code to a console. However, I get a UnhandledPromiseRejectionWarning. My question is why can't I catch an error and send a code to the console?

So This is what I try for the command

const property1 = result.participants.summonerName
            const BResult = property1
            let FResult =  JSON.stringify(BResult)
            message.channel.send(FResult)

and when I try, I got an error says that this person isn't in-game. I know it's false because they are in-game.

So I went further and try to do this instead.

const property1 = result.participants[summonerName]
            const BResult = property1
            let FResult =  JSON.stringify(BResult)
            message.channel.send(FResult)

I still get the same result from the last one. I also try to do const property1 = result.summonerName, but it didn't work as well.

B1ackGX
  • 3
  • 2

1 Answers1

2

Try instead to wrapping the pyke.spectator.getCurrentGameInfoBySummone in a try/catch. This example uses try/catch with the await keyword:

module.exports = {
  name: "match",
  category: "LOL",
  description: "Give Summoner's Data to User",
  run: async (client, message, args) => {
    const username = `${args}`;
    const regionID = "na1";

    return pyke.summoner.getBySummonerName(username, regionID).then((data) => {
        try {
            const result = await pyke.spectator.getCurrentGameInfoBySummoner(`${data.id}`, "na1");
            console.log(result);
        } catch (err) {
            console.error(`${args} isn't in game!`);
        }
    });
  },
};

Otherwise you can try just using a Promise catch for the error:

module.exports = {
  name: "match",
  category: "LOL",
  description: "Give Summoner's Data to User",
  run: async (client, message, args) => {
    const username = `${args}`;
    const regionID = "na1";

    return pyke.summoner.getBySummonerName(username, regionID).then((data) => {
        return pyke.spectator.getCurrentGameInfoBySummoner(`${data.id}`, "na1")
            .then(result => {
                console.log(result);
            })
            .catch(err => {
                console.error(`${args} isn't in game!`)
            }); 
    });
  },
};

You can use JSON.stringify to stringify an object and you can use a variety of different methods such as destructuring to extract specific properties you only want to return in combination with creating/returning a new object:

// extract specific properties from `result`
// This is use ES6 destructuring, but you can use dot notation instead or whatever you prefer
const { property1, property2, property 3 } = result;
// return the stringified object only with the properties you need
const payload = { property1, property2 ,property };
return JSON.stringify(payload)
Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
  • Thanks, it did work but I have another question, how can I make the result to a string. Currently, the result is an object but I want to stringify the object to a string – B1ackGX Jul 18 '20 at 21:43
  • Okay you can use JSON.stringify(objectToStringify) to stringify an object to a string. Otherwise if it’s a single property you can just extract and return that. @B1ackGX – Alexander Staroselsky Jul 18 '20 at 22:08
  • I was able to use the JSON.stringify to stringify an object to a string, but because the result will give me a lot of data, I only want a small portion of the data from it. How may I do that? – B1ackGX Jul 18 '20 at 23:12
  • Sure, I'd identify the specific properties you need of `result` then create a new object that only has the properties that you want to returned from `result`. I'll update the answer to try to show an example. – Alexander Staroselsky Jul 18 '20 at 23:26
  • Thank you, if you need the npm of what I use, feel free to ask me, I'll provide you the name, I'm new to javascript so I don't really know much. – B1ackGX Jul 18 '20 at 23:57
  • I was able to successfully extract data from the object but the data inside the object have several different types of property, how may I do that? – B1ackGX Jul 20 '20 at 02:54
  • @B1ackGX I’m not sure what you are asking. Can you just extract the properties you need then return whatever you need? What is the issue? What is the issue the different types? When you say types you mean string vs number? – Alexander Staroselsky Jul 20 '20 at 03:10
  • So the object contains a different type of properties and for example, I can extract property 1 and property 2 but I want to extract a small portion of property 3 which contains strings and numbers in it, how may I do that? – B1ackGX Jul 20 '20 at 03:51
  • Start with getting the properties using dot notation. Save each property to a variable, as in, `const porperty1 = result.some.path`. Do that for each value you want to extract. Then out those saved variables into a new object that you return. – Alexander Staroselsky Jul 20 '20 at 03:54
  • It didn't work, but still thanks for your help! I got an error when I try to do that! – B1ackGX Jul 20 '20 at 07:18
  • @B1ackGX update your question to show the response then what you specifically tried and what is causing an error. – Alexander Staroselsky Jul 20 '20 at 13:03
  • This is an update: I was able to find the variable for the property using Object.key! Thanks for the help though! – B1ackGX Jul 22 '20 at 00:42