Code Monkey home page Code Monkey logo

311_wk1_day2_axios's Introduction

Setup

Initialize and run the app: npm install && npm start.

Overview

You should see some Jest tests. The goal of this excercise is to make all of the tests pass. Feel free to look at the tests folder and examine the logic of the tests. It's ok if you don't understand all of this logic right now.

Also, please note that the urls for all of the axios calls have been provided for you but you will need to require axios at the top of the src/index.js file to get everything working.

Let's complete the following functions:

discoverMovie

  • The initial call will be made by typing axios(url)

  • Axios is a promise (like fetch) so use the .then function to log the result

  • Take a look in your console and examine what was returned

  • When complete, go back and simply return the original axios call to make the test pass

getMovieById

  • Looking at the result from the last function, you should have seen a lot of properties/data. A lot of that data is not useful to our application.

  • We will return the result similar to the last function but this time append .then and just return result.data

  • The movie title should have been "Reservoir Dogs"

getMovieByIdFailure

  • Look at the fakeId on the first line of this function and notice the comment "FAKE ID HERE". Replace the number 1 with the number 5783

  • This call is designed to fail because the movie id 5783 doesn't exist. When axios encounters a status code above 301 it automatically throws an error so in this function we will need then AND catch statements

  • Start with return axios(url) like usual

  • Then use the .then statement to return the status of the response (if successful)

  • Then use the .catch statement which always starts with the parameter error. Return the status of the response of the error (error.response.status)

  • If successful, the test will have passed after receiving a 404 status code

Final thoughts

Hopefully this exercise has given you some insight into using axios. We should have seen multiple responses and interacted with the different parts of the response object (data, status, etc). We will continue to use axios in the future as we work with (and build our own) APIs

311_wk1_day2_axios's People

Contributors

mxviteri avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.