By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

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

P: 1
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 displayCartTotalwith 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!


Expand|Select|Wrap|Line Numbers
  1.  
  2. <script>
  3.  
  4. "use strict";
  5.  
  6. const startApp = () => {
  7. };
  8.  
  9. const displayCartTotal = ({results}) => results;
  10.  
  11. const fetchBill = () => {
  12. const api = 'https://randomapi.com/006b08a801d82d0c9824dcfdfdfa3b3c';
  13. fetch(api)
  14. .then(res => {
  15. res = JSON.stringfy(res);
  16. return res.json()
  17. })    
  18. .then(data => displayCartTotal(data))
  19. .catch(err => consoole.log(err))    
  20. }
  21.  
  22. </script>
  23.  
  24.  
2 Weeks Ago #1
Share this Question
Share on Google+
1 Reply


gits
Expert Mod 5K+
P: 5,297
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)
2 Weeks Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.