Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angular2数据网格属性';id';不';类型字符串中不存在_Angular_Typescript_Datagrid - Fatal编程技术网

angular2数据网格属性';id';不';类型字符串中不存在

angular2数据网格属性';id';不';类型字符串中不存在,angular,typescript,datagrid,Angular,Typescript,Datagrid,我正在尝试实现angular2 datagrid followig 我的模型: export class Inventory{ active: boolean; constructor(public id: number, public article: string, public description: string, public quantity: number, public price : string

我正在尝试实现angular2 datagrid followig

我的模型:

export class Inventory{
    active: boolean;
    constructor(public id: number,
        public article: string,
        public description: string,
        public quantity: number,
        public price : string,
        public imgURL: string,
        public createdAt: string,
        public updatedAt: string) {
            this.active = true;
    }
}
ngOnInit中的我的组件:

this.getAllInventory();
this.table = new NgDataGridModel<Inventory>([]);
for (let inv in this.allInventories) {
    this.table.items.push(new Inventory(
        inv.id,
        inv.article, 
        inv.description, 
        inv.quantity, 
        inv.price, 
        inv.imgURL, 
        inv.createdAt, 
        inv.updatedAt)
    );
}
这里是调用服务的方法,此方法返回清单的json数组:

getAllInventory(){
    var headers = new Headers();
    headers.append("Content-Type", 'application/json');
    headers.append('Authorization', `Bearer ${this.globalVar.getToken()}`);
    var options = new RequestOptions({ headers: headers });
    var result = this.http
       .get(this.globalVar.getHost() + "inventory", options)
       .map((response: Response) => response.json());
    return result;
}


我的错误:在de for loop中,对象inv位于其atributes中,请告诉我:类型string中不存在属性“id”,所有atributes也是如此。我怎样才能修好它

这意味着Angular 2模型中使用的库存中每个属性的类型与服务器数据的类型不匹配

两种选择

  • 修改与web服务中相同的模型
  • 使用下面的代码并强制转换属性

    getAllInventory(){ 此.inventoryService.getAllInventory()已订阅( 数据=>{ 控制台日志(数据); 让我们看看库存:库存

        data.forEach((item) => {
            tempInventory=new Inventory();
    
            tempInventory.id: _.toInteger(item.id),
            tempInventory.article: item.article.toString(),
            tempInventory.description: item.description.toString(),
            tempInventory.quantity: _.toInteger(item.quantity),
            tempInventory.price : item.price.toString(),
            tempInventory.imgURL: item.imgURL.toString(),
            tempInventory.createdAt: item.createdAt.toString(),
            tempInventory.updatedAt: item.updatedAt.toString() 
    
    
            this.allInventories.push(tempInventory);
        }) 
    console.log(this.allInventories);
    },
    err=>{
        alert ("Error getting inventories");
        console.error(err);
    });
    }
    

  • 你能分享你的
    getAllInventory()吗
    method?它可能会返回
    string
    我在@echonax:DWhere-you-initialize
    this.allInventories
    ?您的代码是否正常工作?在for循环之前,您不会收到
    getAllInventory
    的响应。这是一个异步问题。您的第二个选项看起来不错,但我遇到了这个错误:Can not读取未定义的属性push。我想是因为异步进程可能在。在哪里进行此调用?在Init上?
        data.forEach((item) => {
            tempInventory=new Inventory();
    
            tempInventory.id: _.toInteger(item.id),
            tempInventory.article: item.article.toString(),
            tempInventory.description: item.description.toString(),
            tempInventory.quantity: _.toInteger(item.quantity),
            tempInventory.price : item.price.toString(),
            tempInventory.imgURL: item.imgURL.toString(),
            tempInventory.createdAt: item.createdAt.toString(),
            tempInventory.updatedAt: item.updatedAt.toString() 
    
    
            this.allInventories.push(tempInventory);
        }) 
    console.log(this.allInventories);
    },
    err=>{
        alert ("Error getting inventories");
        console.error(err);
    });
    }