Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays

后端 未结 18 1000
逝去的感伤
逝去的感伤 2020-11-29 05:50

I looked at similar questions, but none of them helped me. I am going to receive an object like the following:

[
  {
    \"id\": 1,
    \"name\": \"Safa\",
          


        
相关标签:
18条回答
  • 2020-11-29 06:21
    <ul>
    <li *ngFor = "let Data of allDataFromAws  | async">
      <pre> {{ Data | json}}</pre>
    </li>
    </ul>
    

    use async to convert allDataFromAws into Array Object....

    0 讨论(0)
  • 2020-11-29 06:25

    You can declare the books (on line 2) as an array:

    title: any = 'List of books are represted in the bookstore';
    books: any = []; 
    constructor(private service:  AppService){
    }
    
    ngOnInit(){
      this.getBookDetails();
    }
    
    getBookDetails() {
        this.service.getBooks().subscribe(books => {
            this.books = books.json();
            console.log(this.books);
        });
    }
    
    0 讨论(0)
  • 2020-11-29 06:26

    For anyone else with this issue that arrives here via Google, please check that the host element of the *ngFor directive is accurate. By this, I mean that I encountered this error and spent a long time researching fixes before realizing that I had put the *ngFor on an ng-template element instead of on my component I wanted to repeat.

    Incorrect

    <ng-template *ngFor=let group of groups$ | async" ...>
      <my-component [prop1]="group.key" ... </my-component>
    <\ng-template>
    

    Correct

    <my-component *ngFor=let group of groups$ | async" [prop1]="group.key" ... </my-component>
    

    I know this is an obvious mistake in hindsight, but I hope an answer here will save someone the headache I now have.

    0 讨论(0)
  • 2020-11-29 06:26

    In you use spring boot with Angular ; make sure that whether you create default

    0 讨论(0)
  • 2020-11-29 06:26

    Here is the solution.

    When you are receiving array from your database. and you are storing array data inside a variable but the variable defined as object. This time you will get the error.

    I am receiving array from database and I'm stroing that array inside a variable 'bannersliders'. 'bannersliders' type is now 'any' but if you write 'bannersliders' is an object. Like bannersliders:any={}. So this time you are storing array data inside object type variable. So you find that error.

    So you have to write variable like 'bannersliders:any;' or 'bannersliders:any=[]'.

    Here I am giving an example.

    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
    bannersliders:any;
      getallbanner(){
        this.bannerService.getallbanner().subscribe(data=>{
          this.bannersliders =data;
       })
      }

    0 讨论(0)
  • 2020-11-29 06:29

    Remove this.requests from

    ngOnInit(){
      this.requests=this._http.getRequest().subscribe(res=>this.requests=res);
    }
    

    to

    ngOnInit(){
      this._http.getRequest().subscribe(res=>this.requests=res);
    }
    

    this._http.getRequest() returns a subscription, not the response value. The response value is assigned by the callback passed to subscribe(...)

    0 讨论(0)
提交回复
热议问题