How do I convert data fetched from a random api to json?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Klem Lloyd
    New Member
    • Aug 2019
    • 1

    #1

    How do I convert data fetched from a random api to json?

    I am quite new to JavaScript and I have a task to finish a certain app, in the process I have the following requirement:

    Create a fetchBill function. It should assign (link I have given in the script) to an api variable. It should then use the browser's fetch function to make a HTTP request to api. Using an arrow function in a .then call to the fetch function, return the response after converting it to JSON. Using an arrow function in another .then call to the first one, take the converted JSON data in a data parameter and call displayCartTota lwith it. Make sure to handle errors that may occur, e.g by showing a warning message in the console.

    Call the fetchBill function from inside startApp.

    What I have tried:

    I have written the following code, but I keep receiving a warning that I haven't declared the fechBill function as required and assigned a correct URL. And that I should check instructions!


    Code:
    <script>
    
    "use strict";
    
    const startApp = () => {
    };
    
    const displayCartTotal = ({results}) => results;
    
    const fetchBill = () => {
    const api = 'https://randomapi.com/006b08a801d82d0c9824dcfdfdfa3b3c';
    fetch(api)
    .then(res => {
    res = JSON.stringfy(res);
    return res.json()
    })	
    .then(data => displayCartTotal(data))
    .catch(err => consoole.log(err))	
    }
    
    </script>
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    when exactly does the error occur? if its when you want to call the function from startApp - then just declare it before this function. (and line 15 and 19 have typos too)
    Last edited by gits; Aug 5 '19, 08:06 AM.

    Comment

    Working...