01-Logging JSON

Now that we've received some data back from the API, we need to start working with it. In this module, we start to write the displayResults() function that will handle the data.

Logging the JSON

Let's first just figure out how to pass this JSON around from our fetch result to another function. Insert the code below. Note that some code has been omitted from before:

function fetchResults(e) {
  //previous code
  fetch(url).then(function(result) {
    return result.json();
  }).then(function(json) {
    displayResults(json); //1 & //3
  });
}

//2
function displayResults(json) {
  console.log("DisplayResults", json); 
};

So we've done a few things in our code here: 1. We've taken out the console.log in our fetch and replaced it with displayResults(json). 2. We moved the console.log to a displayResults() function. 3. Now, just to recap: when the Promise returns the json, we fire off a function called displayResults that will work to manage that data.

This is a common first step as we start to work with JSON data to manipulate the DOM. We're moving the data around so that we can pull it apart in other places. We'll start pulling the JSON apart and preparing to display it in the displayResults function.

Last updated