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
  • fetchResults()
  • Accessing a REST API
  • Endpoint Review
  • Results
  • Further Study
  • Helpful Table
  1. 03-API Fundamentals
  2. 4-Apps
  3. 01-New York Times
  4. 04-fetchResults

01-fetchResults()

In this module, we'll stub out the function that is fired when the 'submit' event happens.

fetchResults()

To get started, it's best to stub out the function. We'll do that under all of our variables as a function declaration:

function fetchResults(e){
    console.log(e);
}

Accessing a REST API

To access the API, we need to make a target request to its URL. Our small search form in the html file contains everything we need to create that url. Enter the following code first, then we'll talk about what exactly it's doing.

                    //1
function fetchResults(e) {
  console.log(e); //2
  // Assemble the full URL
  url = baseURL + '?api-key=' + key + '&page=' + pageNumber + '&q=' + searchTerm.value; //3
  console.log(url); //4
}

function nextPage(){
  console.log("Next button clicked");
} //5

function previousPage(){
  console.log("Next button clicked");
} //5

Here's some analysis of the above code: 1. The little (e) is part of something in Javascript called an event handling function. Every event handling function receives an event object. For the purpose of this discussion, think of an object as a "thing" that holds a bunch of properties (variables) and methods (functions). The handle, the e, is similar to a variable that allows you to interact with the object.

  1. We are logging the event object so that you can look at it in the console for learning purposes.

  2. We are creating a versatile query string. The url variable is loaded with other variables and url requirements. We have our base for the API, our key, the page number that corresponds to the results array, and our specific value. Something to study on your own might be: ?, &, and &q= in a URL string. What are those?

  3. We log the string so that we can see it. Later on, you can try another search and see how it changes.

  4. We add in a few basic functions to define nextPage and previousPage and log them. If you leave out this step, your app will get an error.

Endpoint Review

Remember that when we fetch data from an API, we make a request for some kind of data to a specific point. That point is called an endpoint and comes in the form of a URL. That URL is the gateway to the server, which ultimately does the logic of looking for the proper data in the database. In the code above, we use the endpoint, the baseURL, and add our query string to it so that it can access the proper data.

Results

Here's what we should be seeing at this point:

NOTE: Right now, our button is attempting to submit the search form, but we don't have anywhere for it to send it to. This just causes the page to refresh, which will prevent you from seeing the result shown here. In order to see the search string, go into the settings of the console and select "Preserve Log". This will keep the log after a refresh instead of disposing of it. In the next module, we'll cover a way to prevent the button from trying to submit the form, which will render this step moot.

Further Study

Helpful Table

Here's a helpful table to refer back to when using this API:

Parameter

Purpose

baseURL

The actual web address for the API

apiKey

Your key allowing you access into the API

page

The current page of results being accessed

q

This is the search term, or query, that we are looking for in the API

begin_date

The earliest (furthest away) date from which we want to see articles

end_date

The latest (most recent) date from which we want to see articles

Previous04-fetchResultsNext02-preventDefault()

Last updated 7 years ago

As you're digging into the Event object, do some cross-referencing with this .

source
Fetch Result