I encountered the following error message while attempting to retrieve updated data:
Error trying to diff '[object Object]'. Only arrays and iterables are allowed
Snippet of Get Code:
allDatas
allData(data) {
this.allDatas = data
}
Update Code I Wrote:
updateTodo(currentTodo){
// console.log(currentTodo)
this._todo.updateTask(currentTodo).subscribe(
data=>{console.log(data);this.allData(data)},
error=>console.log(error)
)
}
This Request Originates From Service.ts
updateTask(todo:TodoModel):Observable<TodoModel>{
return this._http.put<TodoModel>('http://127.0.0.1:3000/todo/updateTodo/'+todo.id,todo,headerOption)
}
After thorough debugging with console.log, pinpointed the line where the error occurs:
updateTodo(currentTodo){
// console.log(currentTodo)
this._todo.updateTask(currentTodo).subscribe(
data=>{console.log(data);this.allData(data)}, //Error Comes from this line----------
error=>console.log(error)
)
}
When I adjust the update code to simply data=>console.log(data)
, the error disappears. However, using
data=>{console.log(data);this.allData(data)}
triggers the error:https://i.sstatic.net/iAYDC.jpg
HTML Snippet Where Data is Bound:
<tbody *ngFor="let t of allDatas;let i= index">
<tr class="table-success" *ngIf="t && t.complited">
<td>{{t.task}}</td>
<td>{{t.date.year+"-"+t.date.month+"-"+t.date.day}}</td>
<td> {{t.complited}}</td>
<td>
<i class="fa fa-times-circle btn idelete" style="font-size:25px;" (click)="putTrue(t)"></i>
<i class="fa fa-edit btn iedit" style="font-size:25px;color:rgb(31, 12, 12)" (click)="editTodo(t)"></i>
<i class="fa fa-trash-o btn idelete" style="font-size:25px;" (click)="deleteTask(t.id)"></i>
</td>
</tr>
<tr class="table-danger" *ngIf="t && !t.complited">
<td>{{t.task}}</td>
<td>{{t.date.year+"-"+t.date.month+"-"+t.date.day}}</td>
<td> {{t.complited}}</td>
<td>
<i class="fa fa-check-circle btn idone" style="font-size:25px;" (click)="putTrue(t)"></i>
<i class="fa fa-edit btn iedit" style="font-size:25px;color:rgb(31, 12, 12)" (click)="editTodo(t)"></i>
<i class="fa fa-trash-o btn idelete" style="font-size:25px;" (click)="deleteTask(t.id)"></i>
</td>
</tr>
</tbody>
AllDatas is an array:
https://i.sstatic.net/Utcux.jpg
JSON Data for Operations:
[{"id":29,"task":"random 5","date":{"year":2020,"month":5,"day":9},"category":"genral","complited":false},null,{"id":31,"task":"task 32","date":{"year":2020,"month":5,"day":31},"category":"genral","complited":false}]
In short, encountering an error specifically when updating a task and retrieving the updated task. Same method used for other operations yielding perfect results.