3


i got Error trying to diff '[object Object]'. Only arrays and iterables are allowed this error when i'm trying to get updated data

Get code:

allDatas
  allData(data) {
    this.allDatas = data
  }

Okay So this is Update Code i Write :

updateTodo(currentTodo){  
       // console.log(currentTodo)
        this._todo.updateTask(currentTodo).subscribe(
          data=>{console.log(data);this.allData(data)},
          error=>console.log(error)
        )
  }

This Request Comes 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)
  }

I checked all the things using cosole.log And at the end i got the line from where i got the error let me show you

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)
            )
      }

this is the same update code : when i write data=>console.log(data) in update code then there is no longer error shows but when i use data=>{console.log(data);this.allData(data)} i got this Error:

Error

HTML : where i bind data:

<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>

So allDatas is A array :

enter image description here

JSON Data: Where all oprations are Performed

[{"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}]


so in short i got error when i update task and getting updated task but i use the same method for post put other data i get perfect result
please someone help me i'm trying to solve this error since very long...

8
  • 1
    You need to show the template where the this.allDatas variable is used. It looks like the API is returning an object and you are probably trying to loop over it using *ngFor directive. Commented Apr 30, 2020 at 23:11
  • 1
    Does this answer your question? Error trying to diff '[object Object]' in Angular Commented Apr 30, 2020 at 23:13
  • i think there is no issue of array or object because in post method i use the same approach at there its working fine Commented Apr 30, 2020 at 23:21
  • type of data you get is not an array ?? i saw in console is an sample object Commented Apr 30, 2020 at 23:22
  • i Add HTML code i bind date as Different because of format issue i also check by removing date but it shows the same error Commented Apr 30, 2020 at 23:23

4 Answers 4

0

From what I can understand from your code, you are getting the error because you are assigning an object to an array in allData method.

In the subscription of the PUT request you receive an object and not an array.

This is how your allData method should look like:

allData(data) {
 this.allDatas.push(data);
}
Sign up to request clarification or add additional context in comments.

6 Comments

i use .push method but it push new data every time and its a UPDATE api
Your Output hey i run your code but its not updating my Record it just push the value repeatedly and when i refresh it delete the value with all pushed value and the original record
the PUT request (update) returns in the request body a JSON object and not an array. that object is your updated todo. I assume you're getting that behaviour you mention above because you're using the allData method also for getting all todos. if is that the case, you should keep that method as you already have it and in the subscription of the update request, you should have this.allDatas.push(data) (data, in this case, is the object). if won't work create a stackblitz with a reproduction of your code.
i do this again what you trying to say i totally understand let me show you code and output both code & Output here you find 2 images of code and output so when i click update it push new task every time and when i refresh the page all pushed data are gone because its update in Variable not in actual database (JSON file).
hmm, it seems to me that you are not getting the data from the server as you should. can you post the code on how you retrieve the whole todos from the server? do you get any errors from the server when you're updating the todo? you should also check your network tab to see if the request gives you 200 status code after update.
|
0

Try to initialise allDatas: this.allDatas=[]

updateTodo(currentTodo){  
          // console.log(currentTodo)
            this._todo.updateTask(currentTodo).subscribe(
              data=>{
               this.allDatas=[]
               console.log(data);
               this.allDatas.push(data)
}

            )
      }

But the best solution is to change the return type of your API to an array []

6 Comments

i use .push method but it push new data every time and its a UPDATE api ... Check service.ts its put api
i update the answer init the list before push data in it
Okay i tried this but it push the new data not perform the put(Update) method
[Your Output](imgur.com/a/qSSyj7g when i refresh all pushed items are gone and also delete record which i use to update
I think the error [object object] is resolved now, you still need to find out why the data is not as expected
|
0

This error is related to *ngFor loop in your html file. it basically says that allDatas is not an array.

Verify the type of allDatas using "typeof" make sure it's an array.

EDIT
I saw the second image and it looks like it's an array.
Please also try to validate that array using an online validation tool.. search Google for json validator.

Also move the *ngFor loop from <tbody> to <tr>

good luck

1 Comment

Its an Array type i also attached image that its an array type please check and tell me if i'm wrong
0

try this
if you use this.allDatas = data; the allDatas refresh and 'allDatas' equals 'data'.
if you use this.allDatas.push(data); the 'data' newly added into the 'allDatas'

and first create a array allDatas : Array = [];

   allDatas : Array<any> = [];

updateTodo(currentTodo){  
          // console.log(currentTodo)
            this._todo.updateTask(currentTodo).subscribe(
              data=>{
               console.log(data);
               this.allDatas = data;
});
      }

2 Comments

Hey, i tried this code but its gives error in code errorInCode and then i edit your code and at the end i put [](brackets) but still not working properly output when i click update task it removes all the records from
sory mistack is we dont create an array... please add array like this allDatas : Array<any> = [];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.