I'm just a fan of programming and as such I'm running into a certain problem. The code below gives me the following error:
Cannot read property 'length' of undefined
at computeCourses
If I run the computeCourses () function from the chrome console if it works and I do not understand why, I appreciate your attention.
const urlJson = '../data/data.json'
let coursesRaw
let courses = []
const dataCohorts = () => {
fetch(urlJson)
.then(response => response.json())
.then(response => {
coursesRaw = response
})
}
dataCohorts()
let computeCourses = () => {
for (var i=0; i < coursesRaw.length; i++){
courses.push(coursesRaw[i].id)
}
}
computeCourses()