0

I have successfully figured out how to display singular API data but have faced an issue. - I am trying to use the following API to return the data of "data" using the "following" shown in the next API example.

For this to be completed, two following API's are used.

  • is used to return "data" to be further used to retrieve "Following data" in the API in question.
  • is used to get the "data" to access the "following data" previously.

Sorry, I am very stuck and have no idea what I have done wrong. Below is my attempted code, I hope this is helpful and explained well.

          let response1 = await $.ajax({
            method: 'GET',
            url: '',
          });
          let response2 = await $.ajax({
            method: 'GET',
            url: ''
          });
          let response3 = await $.ajax({
            method: 'GET',
            url: ''
          });
          const result = response3.find(item => item.ID === response2.result === response1.result.codes)
          console.log(result.ID);
        })();
4
  • Hi Rodger, What is the error you are getting? It helps a lot to know both what you want to have happen and what is going wrong. Commented Nov 8, 2019 at 17:04
  • Look at the error in the console. response2 is an array of objects, therefore you need to loop through it and or pull out a specific object in that array. In either case you need to access response2.result by index. Commented Nov 8, 2019 at 17:07
  • @RoryMcCrossan Yes I saw from the browser console but I was unsure how to code that into this example due to being new to ajax etc Commented Nov 8, 2019 at 17:12
  • I cant seem to edit the post so here is the errors @NegativieFriction Uncaught (in promise) TypeError: Cannot read property 'ConstituencyCode' of undefined at msp2test.php:23 at Array.find (<anonymous>) at msp2test.php:23 Commented Nov 8, 2019 at 17:16

1 Answer 1

1

I was able to get results from your $.ajax requests.

let response1 = $.ajax({
  method: 'GET',
  url: 'https://api.postcodes.io/scotland/postcodes/FK15LD',
});

let response2 = $.ajax({
  method: 'GET',
  url: 'https://data.parliament.scot/api/constituencies'
});

function response3(id) {

  return $.ajax({
    method: 'GET',
    url: 'https://data.parliament.scot/api/MemberElectionConstituencyStatuses/' + id
  });
}

let resp3 = response3(2);
resp3.then(function(res3) {
  console.log(res3);
});

// Or
// I'm not sure where you want to get the ID from for your third request, but

response1.then(function(res1) {
  // do something with res1
  response2.then(function() {
    // do something with res2
    let resp3 = response3(2); // or response3(res2[0].id);
    resp3.then(function(res3) {
      // do something with res3
      console.log(res3);
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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