473,326 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 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.  
Aug 4 '19 #1
1 1075
gits
5,390 Expert Mod 4TB
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)
Aug 5 '19 #2

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

Similar topics

1
by: quantass | last post by:
Im using C#, ASP.NET v1.1. My class contains intrinsic types, ArrayList, and XmlDocument types. Some variables have the value NULL. After usng XmlSerializer and the information on:...
1
by: neeraj | last post by:
Hi All Can any give me the code for convert "DataColumn" data type of "DataTable". Even if data table already populated (have data) Actually I am creating one search module which searches the...
2
by: phil.swenson | last post by:
I'm using Prototype.js and would like to convert the contents of an HTML table to JSON. Converting to an array first is fine too. Any thoughts on this? I haven't seen anyone do anything this.......
8
by: =?Utf-8?B?UmljYXJkbyBRdWludGFuaWxsYQ==?= | last post by:
i need to convert data from string to nibble wich (nibble is a four bits representation) As example i have the following code string data1 = "12345678"; so ¿how can i convert this data...
2
by: Yane Maria | last post by:
I have xml data in my vb.net mobile application: customer.xml <dataset> <customers> <customerid>BONAP</customerid> <companyname>Bon app'</companyname> <contactname>Laurence...
1
by: yoshimishu | last post by:
is there some way to convert a Atom to Json using Java Servlets.
1
by: gubbachchi | last post by:
Hi, For my project I have a "add" button, upon clicking it, it will take the user to next page where there will be a text box and list of data displayed below it which is fetched from mysql...
0
by: JonTwend | last post by:
I am working with the Twitter API and finding it great fun and generally easy to use. But I am having trouble getting to the data in the trends/current.json file. I am able to loop through the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.