[英]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
未定义,因此myArray
和response.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.