0

I'm using the Discord API and I want it to send the longitude and latitude of the ISS using the "where the ISS at" API, but if I try and pick an object out of the JSON it comes back empty.

if (message.content.startsWith(`${prefix} were is the iss`)) {
  const https = require('https');

  https.get('https://api.wheretheiss.at/v1/satellites/25544', (resp) => {
    let data = '';

    // A chunk of data has been recieved.
    resp.on('data', (chunk) => {
      data += chunk;
    });

    // The whole response has been received. Print out the result.
    resp.on('end', () => {

      console.log(JSON.parse(data).explanation);
      const {
        lattitude,
        longitude
      } = data;
      console.log(lattitude)
      console.log(longitude)
      message.channel.send(`ok  ${data}`)
    });

  }).on("error", (err) => {
    console.log("Error: " + err.message);
  })
}
0

2 Answers 2

1

Seems like as of now you are trying to pick out latitude and longitude from the string data. You are parsing it to JSON but not reassigning it, as JSON.parse does not parse in place. You can find more on that here.

Here is what you should be doing-:

const {latitude, longitude} = JSON.parse(data);
Sign up to request clarification or add additional context in comments.

Comments

0
  • is your data a JS object when you destruct?
  • spell latitude correctly
  • there is no explanation in a valid response:
{
  "name": "iss",
  "id": 25544,
  "latitude": -48.568896551266,
  "longitude": 153.94349230508,
  "altitude": 436.29840426652,
  "velocity": 27536.388032323,
  "visibility": "eclipsed",
  "footprint": 4589.4699866736,
  "timestamp": 1592575515,
  "daynum": 2459020.0869792,
  "solar_lat": 23.430766332023,
  "solar_lon": 329.0668493775,
  "units": "kilometers"
}

Working code using fetch

fetch('https://api.wheretheiss.at/v1/satellites/25544')
  .then(response => response.json())
  .then(data => {
    const {
      latitude,
      longitude
    } = data;
    console.log(latitude)
    console.log(longitude)
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.