I'm setting up a Angular Service to retrieve dummy data from an API and push the 3 first elements to a TypeScript Class property, but I'm getting this error: Uncaught (in promise): TypeError: Cannot read property 'group' of undefined
Making use of fetch
method and a for
loop, I'm trying to push the 3 first returned objects to a declared, empty property within the class, however, I get an error when trying to push the data. I'm not able to see the cause of the problem.
export class DishService {
group = [];
constructor() {
}
fetchIt() {
fetch('https://jsonplaceholder.typicode.com/todos')
.then(response => response.json())
.then(function(json){
for(var i = 0; i<3; i++){
console.log(json[i])
this.group.push(json[i])
}
})
}
}