Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Response.data how to get a single data value

Here is the code that im using to try get a single element out of the data.response of an api:

client.on("message", msg => {
  var axios = require("axios").default;

var options = {
  method: 'GET',
  url: 'https://dad-jokes.p.rapidapi.com/random/joke',
  headers: {
    'X-RapidAPI-Host': 'dad-jokes.p.rapidapi.com',
    'X-RapidAPI-Key': '0f787e5af5msh468814e0b585173p1cacafjsn7d774dfb44ff'
  }
};
  if(msg.content === "daddy"){
    axios.request(options).then(function (response) {

      console.log(response.data);
      let dataDad = response.body
        msg.channel.send(`${dataDad}`)
  })`

and here is the response that i get from the api:

{
  success: true,
  body: [
    {
      _id: '60dd3729df8a37528bc79b03',
      setup: 'How often do scientists check the table of elements?',
      punchline: 'Periodically',
      type: 'periodical',
      likes: [],
      author: [Object],
      approved: true,
      date: 1618108661,
      NSFW: false
    }
  ]
}

How do I use the "setup" from the response data in my code at msg.channel.send( "the "setup" ) ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I believe, You need to do response.data.body[0].setup in order to get the first objects setup property

msg.channel.send(response.data.body[0].setup);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading