Maxence Vahedi
Maxence Vahedi

Reputation: 23

Extracting value from an object

I'm working on a discord bot, using discord.js.

I've been working on this command for too long without finding a solution, so I come here to ask for help. Here is my problem, maybe it's really simple to solve to you, and that would be great :D I want to create a command that sends a random gif, based on a keyword. I'm using a node module called giphy-random.

        (async () => {
            const API_KEY = "hidden";

            const gif = await giphyRandom(API_KEY, {
                tag: "kawaii"
            });
            console.log(gif)
          }

I would like to be able to get only the value 'url' of the const which is defined by the function (i'm maybe wrong in my words, I'm a beginner) in order to send it in a channel.

Here is my console log

Upvotes: 0

Views: 74

Answers (3)

Husniddin Qurbonboyev
Husniddin Qurbonboyev

Reputation: 1864

You can simply access field like this:

            const API_KEY = "hidden";

            const gif = await giphyRandom(API_KEY, {
                tag: "kawaii"
            });

           const {url} = gif.data; // equal to const url = gif.data.url

            console.log(gif);
          }

Upvotes: 2

KiaiFighter
KiaiFighter

Reputation: 667

According to the docs, the link is returned in data.url property of the resulting object. So your code should look like:

      (async () => {
        const API_KEY = "hidden";

        const gif = await giphyRandom(API_KEY, {
            tag: "kawaii"
        });
        console.log(gif.data.url)
      }

Upvotes: 2

Jamiec
Jamiec

Reputation: 136104

You simply want gif.data.url In fact if you change your console.log like this:

console.log(gif.data.url);

You'll see the url printed to the console.

Upvotes: 2

Related Questions