I find it difficult to overcome “Cannot find a different supporting object” [object Object] ”of type“ object ”, an error that seems really common with Angular2, and I hope that someone comes across something similar.
Here the (anonymous) JSON comes from my service, which is very simple:
[ { "item_id": 1, "item_type": 2, "item_name": "Item 1", "item_description": "First item" }, { "item_id": 2, "item_type": 4, "item_name": "Item 2", "item_description": "Second item" } ]
And here is the contents of my class, service, and component that describes these objects:
And finally, the ngFor component:
<ul> <li *ngFor="let item of items"> <i class="fa fa-database"></i> {{item.item_name}} </li> </ul>
I see nothing wrong with any part of this. The resulting data definitely reaches the component of the component, which means that my import data is correct, and what is displayed in my console.log is definitely an array with the __proto__:Array[0] property __proto__:Array[0] and that’s it. It even looks identical to what is displayed if I console the Angular Heroes tutorial. But he simply will not iterate over the array, insisting that it is an object.
What am I doing wrong? Is ngFor just broken?
Edit Here is the complete (anonymous) class, with the removal of unbound bits:
import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { Headers, Response, Http, RequestOptions } from '@angular/http'; import { Item } from '../../classes/item/item'; import { ItemService } from '../../services/item/item.service'; @Component({ moduleId: module.id, selector: 'my-items', templateUrl: 'items.component.html' }) export class ItemComponent implements OnInit { items: Item[] = []; constructor( private router: Router, private itemService: ItemService ) { } getItems(): void { console.log(this.items); this.itemService .getItems() .then((items) => { console.log(items); this.items = Array.from(items); }); } ngOnInit() { this.getItems(); } }
Edit 2 :
I understood! And I think this might be a bug in Angular2. Above, I cleared my code using a common variable name called "items". But in real, production code, the variable is called "entities." And all this time I called it that. On a whim, I changed the variable name to "testentities" and it worked!
So, to make sure, I tested several options, and it worked every time. Then I changed it to "entities" and the error reappeared. This seems to be a reserved variable.
I will carefully check this, and if it is constantly reproducible, I will report this to the error tracker.