简体   繁体   English

如何修复此错误:无法在 Javascript 中读取未定义的属性(读取“长度”)

[英]How to fix this error : Cannot read properties of undefined (reading 'length') in Javascript

I am new to Javascript.我是 Javascript 新手。 Why is this code giving me the error: Cannot read properties of undefined (reading 'length') ?为什么这段代码给我错误: Cannot read properties of undefined (reading 'length') I have tried to add the datasrc code before but it is not working.我之前尝试过添加 datasrc 代码,但它不起作用。 Does anyone else have an idea on how to fix it?有没有人知道如何解决它?

//implement css in this files
import './style.css';

//jquery already imported
import $ from 'jquery';
// ajax to load

// do table data binding here using data from variable resultArr
var myArray = []

$.ajax({
    method:'GET',
    url:'https://www.seetrustudio.com/data.php',
    success:function(response){
        myArray = response.data
        buildTable(myArray)
        console.log(myArray)
    }
})
function buildTable(data){
    var table = document.getElementById('myTable')

    for (var i = 0; i < data.length; i++){
        var row = `<tr>
                        <td>${data[i].date}</td>
                        <td>${data[i].weather}</td>
                        <td>${data[i].temperature}</td>
                        <td>${data[i].temp_unit}</td>
                  </tr>`
        table.innerHTML += row


    }
}`

There is the only one place in your code where length is used: data.length in for loop.您的代码中只有一个地方使用了length :for 循环中的data.length

for (var i = 0; i < data.length; i++){

So, intepreter tells I cannot read length of undefined hence data is undefined, hence myArray and response.data is undefined and you should set a breakpoint on line因此,解释器告诉I cannot read length of undefined因此data未定义,因此myArrayresponse.data未定义,您应该在线设置断点

myArray = response.data

and see what is in the response, and why it's not contains the key data .并查看响应中的内容,以及为什么它不包含关键data

For me, I have not updated the API key for my app.对我来说,我还没有更新我的应用程序的 API 密钥。 When I have done so, The issue is resolved.当我这样做时,问题就解决了。

暂无
暂无

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

相关问题 如何修复此错误:未捕获(承诺中)类型错误:无法读取未定义的属性(读取“长度”) - how fix this error:Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length') 未处理的运行时错误类型错误:无法读取未定义的属性(读取“长度”) - Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'length') 无法读取代码框中未定义的属性(读取“长度”) - Cannot read properties of undefined (reading 'length') in codesandbox 无法在nodejs中读取未定义的属性(读取“长度”) - Cannot read properties of undefined (reading 'length') in nodejs 类型错误:无法读取未定义的属性(读取“长度”) - TypeError: Cannot read properties of undefined (reading 'length') 如何修复“TypeError:无法读取未定义的属性(读取'useSystemColorMode')” - How to fix "TypeError: Cannot read properties of undefined (reading 'useSystemColorMode')" 如何修复 typeError: cannot read properties of undefined (reading 'upload') in vue - how to fix typeError: cannot read properties of undefined (reading 'upload') in vue 如何解决:无法读取未定义的属性(读取“承诺”)? - How to fix: Cannot read properties of undefined (reading 'Promise')? 如何修复此错误:“类型错误:无法读取 Discordjs 中未定义的属性(正在读取‘toLowerCase’)”? - How do I fix this error: "TypeError: Cannot read properties of undefined (reading 'toLowerCase')" in Discordjs? 如何修复此错误(使用 querySelector):Cannot read properties of undefined (reading 'style') at showSlides - How to fix this error (querySelector is used): Cannot read properties of undefined (reading 'style') at showSlides
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM