简体   繁体   English

当我想获取数据时出现此错误:TypeError:无法读取未定义的属性“数据”

[英]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.

相关问题 我有这个错误: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'buffer' of undefined - I have this error: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'buffer' of undefined 我有错误未捕获的TypeError:无法读取未定义的属性&#39;toLowerCase&#39; - I have the error Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 当我想访问对象详细信息时,React JS引发“ TypeError:无法读取未定义的属性&#39;名称&#39;”错误 - React JS throw “TypeError: Cannot read property 'name' of undefined” error when i want to access object details 我在 axios 补丁 api 中收到“Uncaught (in promise) TypeError: Cannot read property 'data' of undefined”错误 - I'm getting “Uncaught (in promise) TypeError: Cannot read property 'data' of undefined” error in axios patch api 我试图 map 数据库中的数据并收到此错误。 TypeError:无法读取未定义的属性“地图” - I trying to map the data from the database and getting this error. TypeError: Cannot read property 'map' of undefined 使用节点 JS 向 MongoDB 添加数据,我收到此错误:“TypeError: Cannot read property 'username' of undefined” - Adding data to MongoDB using node JS and I get this error: “TypeError: Cannot read property 'username' of undefined” 收到TypeError:尝试包含我用AngularJS制作的AuthInterceptor服务时,无法读取未定义的属性“ data” - Getting TypeError: Cannot read property 'data' of undefined, when trying to include an AuthInterceptor service I made with AngularJS 类型错误:当我尝试访问 API 调用数据时,无法读取未定义的属性“映射” - TypeError: Cannot read property 'map' of undefined when I am trying to access API call data 如何防止 TypeError:提交空表单数据时无法读取未定义的属性“地图”? - How can I prevent TypeError: Cannot read property 'map' of undefined when submitting empty form data? 类型错误:无法读取未定义的属性“发送”。当我收到此类错误并想保留当前设置时该怎么办 - TypeError: Cannot read property 'send' of undefined .What should I do when I get such an error and want to keep the currents settings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM