import data into H2 via API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fra1983
    New Member
    • Oct 2021
    • 6

    import data into H2 via API

    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

    Code:
    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title></title>
        <script>
          const intervalTime = 10 * 1000;
         const container = document.getElementsByClassName("u-text u-text-palette-3-base u-title u-text-3");
        const endpoint =      "https://*************/v0/*******/************************/collections/****************?offset=0&limit=unlikely";
        function onInterval() {
          fetch(endpoint, {
            method: "GET",
            headers: { Authorization: "Bearer **********************" }
          })
            .then((res) => res.json())
            .then((data) => {
              const title = data.records.map((a) => {
                return{
                  close: (a.close)
                };
              });
              const lastClose = title[title.length - 1];
            console.log(lastClose);
            });
          }
          onInterval();
          window.setInterval(onInterval, intervalTime);
      </script>
      </head>
      <body>
        <h2><p class="u-text u-text-palette-3-base u-title u-text-3"></p>
        </h2>
      </body>
    </html>
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    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?

    Comment

    • fra1983
      New Member
      • Oct 2021
      • 6

      #3
      yes, I rewrote the code but it not only displays the number but all the json data {"$": 2.2475629277535 063}. I just want the number 2.24
      but not being an expert I don't know how to do it ..
      Code:
      <!DOCTYPE html>
      <html>
      <body>
      <h2 id="demo"></h2>
      <script>
      
        fetch(`https://********************/v0/******************/******************/collections/t_e*******************0?offset=0&limit=unlikely`, {
      
      method: "GET",
      headers:{'Authorization' : 'Bearer *********************'}
      * * *})
      .then((res) => res.json())
      .then(data => {const cdata = data.records.map(a => {
       return{$ :parseFloat(a.close)}
      
      });
      var myJSON = JSON.stringify(cdata[cdata.length - 1]);
      
      document.getElementById("demo").innerHTML = myJSON;
      
      
      }
      
      );
      
      
      </script>
      </body>
      </html>

      Comment

      • dev7060
        Recognized Expert Contributor
        • Mar 2017
        • 655

        #4
        yes, I rewrote the code but it not only displays the number but all the json data {"$": 2.2475629277535 063}. 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).

        Comment

        • fra1983
          New Member
          • Oct 2021
          • 6

          #5
          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()
          propertyIsEnume rable: ƒ propertyIsEnume rable()
          toLocaleString: ƒ toLocaleString( )
          toString: ƒ toString()
          valueOf: ƒ valueOf()
          __defineGetter_ _: ƒ __defineGetter_ _()
          __defineSetter_ _: ƒ __defineSetter_ _()
          __lookupGetter_ _: ƒ __lookupGetter_ _()
          __lookupSetter_ _: ƒ __lookupSetter_ _()
          __proto__: (...)
          get __proto__: ƒ __proto__()

          Comment

          • dev7060
            Recognized Expert Contributor
            • Mar 2017
            • 655

            #6
            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()
            propertyIsEnume rable: ƒ propertyIsEnume rable()
            toLocaleString: ƒ toLocaleString( )
            toString: ƒ toString()
            valueOf: ƒ valueOf()
            __defineGetter_ _: ƒ __defineGetter_ _()
            __defineSetter_ _: ƒ __defineSetter_ _()
            __lookupGetter_ _: ƒ __lookupGetter_ _()
            __lookupSetter_ _: ƒ __lookupSetter_ _()
            __proto__: (...)
            get __proto__: ƒ __proto__()
            Code:
            var myJSON =  '{"$": 2.2475629277535063}' ;
            const obj = JSON.parse(myJSON);
            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.
            Code:
            var cdata = [{'$': 2.2475629277535063}, {'$': 5.552475629277535063}];
            console.log(cdata[0]['$'].toFixed(2));
            Attached Files

            Comment

            • fra1983
              New Member
              • Oct 2021
              • 6

              #7
              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

              Code:
              <html>
              <body>
              <h2 id="demo"></h2>
              <script>
              
                fetch(`https://****************/collections/t**************1j0?offset=0&limit=unlikely`, {
              
              method: "GET",
              headers:{'Authorization' : 'Bearer **************'}
              * * *})
              .then((res) => res.json())
              .then(data => {const cdata = data.records.map(a => {
              return{$ :parseFloat(a.close).toFixed(2)}
              
              });
              
              const obj =JSON.parse(cdata)
              document.getElementById("demo").innerHTML = `${obj [0] ['$']}`;
              
              
              });
              
              
              </script>
              </body>
              </html>

              Comment

              • dev7060
                Recognized Expert Contributor
                • Mar 2017
                • 655

                #8
                Code:
                <html>
                <body>
                <h2 id="demo"></h2>
                <script>
                fetch(`...`, {
                
                        method: "GET",
                        headers: {
                            'Authorization': 'Bearer ...'
                        }
                    })
                    .then((res) => res.json())
                    .then(data => {
                        const cdata = data.records.map(a => {
                            return {
                                $: parseFloat(a.close)
                            }
                
                        });
                		document.getElementById("demo").innerHTML = cdata[0]['$'].toFixed(2);
                    });
                 
                </script>
                </body>
                </html>
                You may not want to reveal the authorization token publicly.

                Comment

                • fra1983
                  New Member
                  • Oct 2021
                  • 6

                  #9
                  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

                  Comment

                  • dev7060
                    Recognized Expert Contributor
                    • Mar 2017
                    • 655

                    #10
                    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.
                    Code:
                    <html>
                    <body>
                    <h2 id="demo"></h2>
                    <script>
                    fetch(`...`, {
                     
                            method: "GET",
                            headers: {
                                'Authorization': 'Bearer ...'
                            }
                        })
                        .then((res) => res.json())
                        .then(data => {
                            const cdata = data.records.map(a => {
                                return {
                                    $: parseFloat(a.close)
                                }
                     
                            });
                            var index = cdata.length-1;
                    		document.getElementById("demo").innerHTML = cdata[index]['$'].toFixed(2);
                        });
                     
                    </script>
                    </body>
                    </html>

                    Comment

                    • fra1983
                      New Member
                      • Oct 2021
                      • 6

                      #11
                      thank you thank you thank you ... thank you for all the time you spent on me. you are great

                      Comment

                      Working...