469,300 Members | 2,191 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

import data into H2 via API

6 Nibble
I'm a programming beginner. . Now I'm having a problem that I struggle to solve ... I want to write a numeric data in the H2 tag that I import via API call. I have tried in various ways, making a console log the data exists but it seems that I am wrong something to richamarlo in H2. I seek help in understanding and resolving this error. Thank you

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html lang="en" dir="ltr">
  3.   <head>
  4.     <meta charset="utf-8">
  5.     <title></title>
  6.     <script>
  7.       const intervalTime = 10 * 1000;
  8.      const container = document.getElementsByClassName("u-text u-text-palette-3-base u-title u-text-3");
  9.     const endpoint =      "https://*************/v0/*******/************************/collections/****************?offset=0&limit=unlikely";
  10.     function onInterval() {
  11.       fetch(endpoint, {
  12.         method: "GET",
  13.         headers: { Authorization: "Bearer **********************" }
  14.       })
  15.         .then((res) => res.json())
  16.         .then((data) => {
  17.           const title = data.records.map((a) => {
  18.             return{
  19.               close: (a.close)
  20.             };
  21.           });
  22.           const lastClose = title[title.length - 1];
  23.         console.log(lastClose);
  24.         });
  25.       }
  26.       onInterval();
  27.       window.setInterval(onInterval, intervalTime);
  28.   </script>
  29.   </head>
  30.   <body>
  31.     <h2><p class="u-text u-text-palette-3-base u-title u-text-3"></p>
  32.     </h2>
  33.   </body>
  34. </html>
  35.  
2 Weeks Ago #1
10 6239
dev7060
501 Expert 256MB
but it seems that I am wrong something to richamarlo in H2
What do you mean?

I want to write a numeric data in the H2 tag that I import via API call
Did you try using innerHTML property?
2 Weeks Ago #2
fra1983
6 Nibble
yes, I rewrote the code but it not only displays the number but all the json data {"$": 2.2475629277535063}. I just want the number 2.24
but not being an expert I don't know how to do it ..
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h2 id="demo"></h2>
  5. <script>
  6.  
  7.   fetch(`https://********************/v0/******************/******************/collections/t_e*******************0?offset=0&limit=unlikely`, {
  8.  
  9. method: "GET",
  10. headers:{'Authorization' : 'Bearer *********************'}
  11. * * *})
  12. .then((res) => res.json())
  13. .then(data => {const cdata = data.records.map(a => {
  14.  return{$ :parseFloat(a.close)}
  15.  
  16. });
  17. var myJSON = JSON.stringify(cdata[cdata.length - 1]);
  18.  
  19. document.getElementById("demo").innerHTML = myJSON;
  20.  
  21.  
  22. }
  23.  
  24. );
  25.  
  26.  
  27. </script>
  28. </body>
  29. </html>
  30.  
  31.  
2 Weeks Ago #3
dev7060
501 Expert 256MB
yes, I rewrote the code but it not only displays the number but all the json data {"$": 2.2475629277535063}. I just want the number 2.24
but not being an expert I don't know how to do it ..
Parse the JSON using JSON.parse(). To truncate the decimals, you could use toFixed(2) (rounds off the value).
2 Weeks Ago #4
fra1983
6 Nibble
hello thanks for the suggestions .. I entered the toFixed (2) and it works ... but I can't get just the number. doing a consol log () I get this.
I honestly don't know how to do it, you can help me



{$: '2.28'}
$: "2.28"
[[Prototype]]: Object
constructor: Object()
hasOwnProperty: hasOwnProperty()
isPrototypeOf: isPrototypeOf()
propertyIsEnumerable: propertyIsEnumerable()
toLocaleString: toLocaleString()
toString: toString()
valueOf: valueOf()
__defineGetter__: __defineGetter__()
__defineSetter__: __defineSetter__()
__lookupGetter__: __lookupGetter__()
__lookupSetter__: __lookupSetter__()
__proto__: (...)
get __proto__: __proto__()
2 Weeks Ago #5
dev7060
501 Expert 256MB
hello thanks for the suggestions .. I entered the toFixed (2) and it works ... but I can't get just the number. doing a consol log () I get this.
I honestly don't know how to do it, you can help me



{$: '2.28'}
$: "2.28"
[[Prototype]]: Object
constructor: Object()
hasOwnProperty: hasOwnProperty()
isPrototypeOf: isPrototypeOf()
propertyIsEnumerable: propertyIsEnumerable()
toLocaleString: toLocaleString()
toString: toString()
valueOf: valueOf()
__defineGetter__: __defineGetter__()
__defineSetter__: __defineSetter__()
__lookupGetter__: __lookupGetter__()
__lookupSetter__: __lookupSetter__()
__proto__: (...)
get __proto__: __proto__()
Expand|Select|Wrap|Line Numbers
  1. var myJSON =  '{"$": 2.2475629277535063}' ;
  2. const obj = JSON.parse(myJSON);
  3. console.log(obj['$'].toFixed(2));



You may not need to stringify it then parse it. If 'cdata' is something like this, you should be able to access the object.
Expand|Select|Wrap|Line Numbers
  1. var cdata = [{'$': 2.2475629277535063}, {'$': 5.552475629277535063}];
  2. console.log(cdata[0]['$'].toFixed(2));

Attached Images
File Type: png dev7060.PNG (5.4 KB, 75 views)
File Type: png dev7060_2.PNG (5.2 KB, 76 views)
2 Weeks Ago #6
fra1983
6 Nibble
hello, thanks for the suggestions, but there is something wrong .. I am attaching the complete code ... I don't know who to contact. Thanks for all

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <h2 id="demo"></h2>
  4. <script>
  5.  
  6.   fetch(`https://****************/collections/t**************1j0?offset=0&limit=unlikely`, {
  7.  
  8. method: "GET",
  9. headers:{'Authorization' : 'Bearer **************'}
  10. * * *})
  11. .then((res) => res.json())
  12. .then(data => {const cdata = data.records.map(a => {
  13. return{$ :parseFloat(a.close).toFixed(2)}
  14.  
  15. });
  16.  
  17. const obj =JSON.parse(cdata)
  18. document.getElementById("demo").innerHTML = `${obj [0] ['$']}`;
  19.  
  20.  
  21. });
  22.  
  23.  
  24. </script>
  25. </body>
  26. </html>
  27.  
  28.  
2 Weeks Ago #7
dev7060
501 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <h2 id="demo"></h2>
  4. <script>
  5. fetch(`...`, {
  6.  
  7.         method: "GET",
  8.         headers: {
  9.             'Authorization': 'Bearer ...'
  10.         }
  11.     })
  12.     .then((res) => res.json())
  13.     .then(data => {
  14.         const cdata = data.records.map(a => {
  15.             return {
  16.                 $: parseFloat(a.close)
  17.             }
  18.  
  19.         });
  20.         document.getElementById("demo").innerHTML = cdata[0]['$'].toFixed(2);
  21.     });
  22.  
  23. </script>
  24. </body>
  25. </html>
You may not want to reveal the authorization token publicly.
1 Week Ago #8
fra1983
6 Nibble
thanks, but it is not correct ... the data that is in json is 2.28 (since it changes every 5 minutes) while the code gives me 0.99 ... I also found this thing ... I do not understand
1 Week Ago #9
dev7060
501 Expert 256MB
I demonstrated a way to read an object from the array.
thanks, but it is not correct ... the data that is in json is 2.28 (since it changes every 5 minutes) while the code gives me 0.99 ... I also found this thing ... I do not understand
'2.28' is not the only data the array has. One could change the index number to read the desired value. From #3, it appears you may want to access the last index.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <h2 id="demo"></h2>
  4. <script>
  5. fetch(`...`, {
  6.  
  7.         method: "GET",
  8.         headers: {
  9.             'Authorization': 'Bearer ...'
  10.         }
  11.     })
  12.     .then((res) => res.json())
  13.     .then(data => {
  14.         const cdata = data.records.map(a => {
  15.             return {
  16.                 $: parseFloat(a.close)
  17.             }
  18.  
  19.         });
  20.         var index = cdata.length-1;
  21.         document.getElementById("demo").innerHTML = cdata[index]['$'].toFixed(2);
  22.     });
  23.  
  24. </script>
  25. </body>
  26. </html>
1 Week Ago #10
fra1983
6 Nibble
thank you thank you thank you ... thank you for all the time you spent on me. you are great
1 Week Ago #11

Post your reply

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

Similar topics

3 posts views Thread by John Kolvereid | last post: by
3 posts views Thread by Schultz | last post: by
4 posts views Thread by N/A | last post: by
reply views Thread by nuray | last post: by
9 posts views Thread by a | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.