简体   繁体   English

如何将 API 响应数据拉入 HTML 表

[英]How to pull a API Response data into a HTML Table

i'm trying to pull data from a response i'm getting on a Google Workspace API request to a HTML table, the response i'm getting is an array that is being store at an HTML element every time I made the request.我正在尝试从我在 Google Workspace API 请求中获得的响应中提取数据到 HTML 表,我得到的响应是一个数组,每次发出请求时都存储在 Z4C4AD5FCA2E7A3F74DBB1CED00381AAZ 元素中。 Here's my code for it:这是我的代码:

  function execute() {
     return gapi.client.directory.users.list({"domain": "mydomain", "maxResults": 450})
            .then(function(response) {
                let t = document.querySelector("#tabela") 
               let data = []    
                console.log(response)
                    response.result.users.forEach((user)=> {
                       
                        data.push(user) 
                        // console.log("Email"+ user.primaryEmail) 
                        // console.log("Email"+ user.lastLoginTime) 
                     SheetDB.write('https://sheetdb.io/api/v1/81m3qdtu47hra', { sheet: 'Sheet1', data:  {email: '${user.primaryEmail}' , login: '${user.lastLoginTime}'}}).then(function(result){
                     console.log(result);
                    }, function(error){
                     console.log(error);
    });
                    })
                    console.log(data)
                    let td = data.reduce((acc, user)=>{
                      acc += `<tr>${user.lastLoginTime}</tr>`
              
                      return acc
                  },"")
                  t.innerHTML = td
                  console.log("O result é:", td)
                  },
                  function(err) { console.error("Execute error", err); });`

and then, my code which is not working to populate my html table with it:然后,我的代码无法用它填充我的 html 表:

     <table class="table table-striped">
        <tr  class="bg-info">
            <th>E-mail</th>
            <th>Last Login Time</th>
        <th>Status</th>
        <th>Permissions</th>
        </tr>
    
        <tbody id="myTable">
            
        </tbody>
    </table>
    
    <script>
        var myArray = document.querySelector("#tabela")
        
        buildTable(myArray)
    
    
    
        function buildTable(data){
            var table = document.getElementById('myTable')
    
            for (var i = 0; i < data.length; i++){
                var row = `<tr>
                                <td>${user.primaryEmail}</td>
                          </tr>`
                table.innerHTML += row
    
    
            }
        }

what am I doing wrong?我究竟做错了什么?

The buildTable function requires an array as the argument to the function whereas you are passing the entire myTable element as the argument on the second line in your script tag. buildTable function 需要一个数组作为 function 的参数,而您将整个myTable元素作为脚本标签第二行的参数传递。 That is why the for loop never gets executed since data.length would be undefined.这就是为什么 for 循环永远不会执行的原因,因为data.length将是未定义的。 Pass the array of data from the API to the function and you should be good to go.将数据数组从 API 传递到 function,您应该对 go 很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM