[英]When i want to fetch data i have this error : TypeError: Cannot read property 'data' of undefined
I want to retrieve API data and display its data on my HTML page all in a table.我想检索 API 数据并将其数据显示在我的 HTML 页面上的所有表格中。
I am getting this error while trying to display my data on my HTML page尝试在我的 HTML 页面上显示我的数据时出现此错误
When I try to use the .map当我尝试使用 .map
When i want to fetch data i have this error : TypeError: Cannot read property 'data' of undefined When i want to fetch data i have this error : TypeError: Cannot read property 'data' of undefined当我想获取数据时出现此错误:TypeError:无法读取未定义的属性“数据”当我想获取数据时出现此错误:TypeError:无法读取未定义的属性“数据”
// Convert csv to Json
function csvJSON(csv) {
const lines = csv.split('\n')
const result = []
const headers = lines[0].split(',')
for (let i = 1; i < lines.length; i++) {
if (!lines[i])
continue
const obj = {}
const currentline = lines[i].split(',')
for (let j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j]
}
result.push(obj)
}
return result
}
url = "https://api7.esv2.com/v2/Api/Bounces?apiKey=xxxxxxxxxxxxxxxx&startDate=2020-10-25&endDate=2020-10-25&bounceType=1"
// Fetch Data
function fetchData() {
fetch(url)
.then(response => {
if (!response.ok) {
throw Error("ERROR");
}
$(document).ready(function() {
$.ajax({url}).then(function(data) {
return console.log(csvJSON(data));
})
})
})
.then(data => {
console.log(data);
const html = data.data.map(user => {
return `
<div class="user">
<p>Date:${user.Date}</p>
<p>Email:${user.Email} </p>
<p>BounceType:${user.BounceType}</p>
</div>
`;
}).join("");
console.log(html);
document.querySelector('#app')
.insertAdjacentHTML("afterbegin", html)
})
.catch(error => {
console.log(error);
});
}
fetchData();
```
You are returning console.log
in the function, try returning without console.log您正在函数中返回console.log
,请尝试在没有 console.log 的情况下返回
$(document).ready(function() {
$.ajax({ url }).then(function(data) {
return csvJSON(data); // changed
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.