简体   繁体   English

表中显示未定义的数据

[英]Data showing undefined in table

I am trying to get my data into an HTML table.我正在尝试将我的数据放入 HTML 表中。 The JavaScript is properly detecting the data and is showing two rows in the table, which is correct. JavaScript 正确检测数据并在表中显示两行,这是正确的。 The issue is each of the fields of both rows display undefined.问题是两行的每个字段都显示未定义。 Not sure where to go from here.不知道从这里去哪里。

 $(function() { var records = []; $.getJSON('https://v1.nocodeapi.com/devmarq/airtable/RiBnzHulmTPHkgnD?tableName=JobListings&fields=company,logo,companyDescription,role,roleDescription,location,link,dateAdded&view=AllListings', function(data) { $.each(data.records, function parseJSON(i, f) { var tblRow = "<tr>" + "<td>" + f.company + "</td>" + "<td>" + f.companyDescription + "</td>" + "<td>" + f.role + "</td>" + "<td>" + f.roleDescription + "</td>" + "</tr>" $(tblRow).appendTo("#userdata tbody"); }); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <div class="wrapper"> <div class="profile"> <table id="userdata" border="2"> <thead> <th>Company</th> <th>Company Description</th> <th>Role</th> <th>Role Description</th> </thead> <tbody> </tbody> </table> </div> </div>

Again the table is showing two rows, which is correct, but each value of the rows says undefined.该表再次显示两行,这是正确的,但行的每个值都表示未定义。

You need to pull the fields key out of the record object:您需要从记录对象中提取字段键:

 $(function() { var records = []; $.getJSON('https://v1.nocodeapi.com/devmarq/airtable/RiBnzHulmTPHkgnD?tableName=JobListings&fields=company,logo,companyDescription,role,roleDescription,location,link,dateAdded&view=AllListings', function(data) { $.each(data.records, function parseJSON(i, { fields: f }) { var tblRow = "<tr>" + "<td>" + f.company + "</td>" + "<td>" + f.companyDescription + "</td>" + "<td>" + f.role + "</td>" + "<td>" + f.roleDescription + "</td>" + "</tr>" $(tblRow).appendTo("#userdata tbody"); }); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <div class="wrapper"> <div class="profile"> <table id="userdata" border="2"> <thead> <th>Company</th> <th>Company Description</th> <th>Role</th> <th>Role Description</th> </thead> <tbody> </tbody> </table> </div> </div>

I changed the second argument of the parseJSON function from f to { fields: f } ;我将parseJSON函数的第二个参数从f更改为{ fields: f } ; this uses object destructuring to grab the value of the fields entry in the object passed to the second argument and assigns it to the f variable so that the rest of the code can use that object to get their values.这使用对象解构来获取传递给第二个参数的对象中的字段条目的值,并将其分配给 f 变量,以便其余代码可以使用该对象来获取它们的值。

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

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