1
function fetchAPI(string) {
  return fetch(string)
    .then(function(response) {
      return response.json();
    })
    .then(function(json) {
      return json;
    });
}

try {
  fetchAPI(`https://api.hypixel.net/skyblock/auctions?key=${apikey}`).then(
    function(result1) {
      delete result1.success;
      delete result1.page;
      delete result1.totalauctions;
      delete result1.lastupdated;
      var pages = result1.totalpages;
      delete result1.totalpages;
      for (page = 0; page < pages; page++) {
        fetchAPI(
          `https://api.hypixel.net/skyblock/auctions?key=${apikey}&page=${page}`
        ).then(function(results) {
          delete results.success;
          delete results.page;
          delete results.totalauctions;
          delete results.lastupdated;
          delete results.totalpages;
          var i = 0;
          for (i = 0; i < results.auctions.length; i++) {
            if (results.auctions[i].item_name != "Enchanted Book") {
              delete results.auctions[i];
            }
          }
          console.log(results);
        });
      }
    }
  );
} catch (error) {
  console.log(error);
}

The code queries an API (api.hypixel.net) and it should filter the response (item_name: 'Enchanted Book'), log the variable to the console, then repeat for the next page (each page = different API query)

Like the title says, when running the code doesn't do anything but doesn't throw an error. Any reason this is?

Maxim Mazurok
  • 3,856
  • 2
  • 22
  • 37
  • the backtick "`" is not equal to single quote. Please double check. – Raptor Sep 24 '20 at 02:40
  • I used a backtick due to the fact that there's a variable (apikey, which is a sting) which is in the URL. Is there another way to do this? (Clarifying, apikey is assigned a string value, I'm using that string value in the URL. apikey can be changed at any time, so I need it to be a variable. –  Sep 24 '20 at 02:43
  • my bad, your code is still correct (Read [this](https://stackoverflow.com/questions/27678052/usage-of-the-backtick-character-in-javascript)). You can alternatively use `'https://api.hypixel.net/skyblock/auctions?key=' + apikey + '&page=' + page`. – Raptor Sep 24 '20 at 02:49

1 Answers1

2

You just have to use the right property name. For example, totalPages, not totalpages (case matters).

Try this:

function fetchAPI(string) {
  return fetch(string)
    .then(function(response) {
      return response.json();
    })
    .then(function(json) {
      return json;
    });
}

try {
  fetchAPI(`https://api.hypixel.net/skyblock/auctions?key=${apikey}`).then(
    function(result1) {
      console.log(result1);
      delete result1.success;
      delete result1.page;
      delete result1.totalAuctions;
      delete result1.lastUpdated;
      var pages = result1.totalPages;
      delete result1.totalPages;
      for (var page = 0; page < pages; page++) {
        fetchAPI(
          `https://api.hypixel.net/skyblock/auctions?key=${apikey}&page=${page}`
        ).then(function(results) {
          delete results.success;
          delete results.page;
          delete results.totalAuctions;
          delete results.lastUpdated;
          delete results.totalPages;
          var i = 0;
          for (i = 0; i < results.auctions.length; i++) {
            if (results.auctions[i].item_name != "Enchanted Book") {
              delete results.auctions[i];
            }
          }
          console.log(results);
        });
      }
    }
  );
} catch (error) {
  console.log(error);
}
Maxim Mazurok
  • 3,856
  • 2
  • 22
  • 37