JS-151-API
  • JavaScript Library
  • 02-DOM-Manipulation
    • Overview
    • DOM Explained
    • Example Setup
    • Selecting Elements
    • Events
    • Complete Example
  • 03-API Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-Intro-To-APIs
      • Intro
      • Client
      • Requests
      • JSON
      • API Endpoints
      • Server
      • Response
      • Statelessness
      • REST
    • 2-Asynchronous-Programming
      • Intro
      • Callbacks
      • Promises
      • Promises Continued
      • Chaining Promises
    • 3-Fetch
      • Fetch Intro
      • Star Wars
        • Star Wars API
        • Star Wars Setup
        • Star Wars JS
      • Random Photo
        • Unsplash
        • Unsplash Setup
        • Unsplash JS
    • 4-Apps
      • 01-New York Times
        • 00-App Intro
        • 01-HTML/CSS/API Key
        • 02-Variables
        • 03-Event Listeners
        • 04-fetchResults
          • 01-fetchResults()
          • 02-preventDefault()
          • 03-fetch() method
          • 04-Dates
        • 05-displayResults
          • 01-Logging JSON
          • 02-Working with JSON
          • 03-Link Display
          • 04-Results Navigation
          • 05-Results Update
          • 06-Keywords
          • 07-Images
        • 06-Pagination
          • 01-Pagination Intro
          • 02-Pagination Functions
        • 07-Next Steps
      • 02-YouTube
        • html
        • youtube.css
        • youtube.js
      • 03-GoogleMaps
        • Setup
        • HTML and CSS files
        • API Key
        • JS Setup
        • Adding Your Location
        • Listeners
        • Custom Options
Powered by GitBook
On this page
  1. 03-API Fundamentals
  2. 4-Apps
  3. 01-New York Times
  4. 05-displayResults

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.

Previous05-displayResultsNext02-Working with JSON

Last updated 7 years ago