error is in for loop in my component. It say that data isn't iterable, but it's an array. How can I fix it; I was trying to do something like
this.httpService.getUser().subscribe((data: Task[]) => this.task = data);
but it says :
error: object access via string literals is disallowed
I have an interface
Task.ts
export interface Task {
taskCategory: string;
taskTitle: string;
taskDescription: string;
taskCompleted: boolean;
}
I have a task.json
{
"todoList": [
{
"taskCategory": "frontend",
"taskTitle": "fix a bug",
"taskDescription": "we have a bug with drag and drop in todo table, fix it",
"taskCompleted": false
},
{
"taskCategory": "back-end",
"taskTitle": "send data",
"taskDescription": "send data from back-end",
"taskCompleted": false
}
]
}
My http.service.ts is :
@Injectable({
providedIn: 'root'
}
)
export class HttpService {
#url = '/assets/task.json';
constructor(private http: HttpClient) { }
getUser(): Observable<Task[]> {
return this.http.get<Task[]>(this.#url);
}
}
and a component
export class AppComponent implements OnInit {
title = 'http';
task: Task[] = [];
constructor(private httpService: HttpService) { }
user: User[] = [];
ngOnInit() {
this.httpService.getUser().subscribe((data: Task[]) => {
for (let item of data) {
this.task = [{
taskCategory: item.taskCategory,
taskCompleted: item.taskCompleted,
taskDescription: item.taskDescription,
taskTitle: item.taskTitle
}]
}
});
}
}
html
<div *ngFor="let item of task; index as i">
{{item.taskCategory}}
</div>
return this.http.get<Task[]>(this.#url).pipe(pluck('todoList'));