Javascript для ошибки цикла не пойманная ошибка типа на .length
В настоящее время я выполняю школьное задание. Задача состоит в том, чтобы сделать простое приложение для постинга в социальных сетях, используя Django и JavaScript. JavaScript используется для того, чтобы динамически загружать посты на веб-страницу и заменять части HTML. Я следовал уроку на YouTube https://youtu.be/f1R_bykXHGE, который должен был мне помочь. Несмотря на то, что я выполнил урок один за другим, я получаю следующую ошибку Uncaught TypeError: Cannot read properties of undefined (reading 'length')at XMLHttpRequest.xhr.onload ((index):63:28).
const postsElement = document.getElementById("posts") // get an html element
// postsElement.innerHTML = 'Loading...' // set new html in that element
// var el1 = "<h1>Hi there 1</h1>"
// var el2 = "<h1>Hi there 2</h1>"
// var el3 = "<h1>Hi there 3</h1>"
// postsElement.innerHTML = el1 + el2 + el3
const xhr = new XMLHttpRequest()
const method = 'GET' // "POST"
const url = "/posts"
const responseType = "json"
xhr.responseType = responseType
xhr.open(method, url)
xhr.onload = function() {
const serverResponse = xhr.response
const listedItems = serverResponse.response // array
var finalPostStr = ""
var i;
for (i=0;i<listedItems.length;i++) {
console.log(i)
console.log(listedItems[i])
}
}
xhr.send()
</script>
Поскольку свойство не определено, возможно, вы неправильно написали 'response' в вашем файле views.py.
