.map不是Angular 2导入地图运算符中的函数不起作用

时间:2017-06-15 22:20:38

标签: angular typescript

这是我的代码,我无法弄清楚为什么这会说.map不是一个函数。这是一种通用方法。

  public findTeamRoles(id: any, userId: any): Observable<any> {
let _method: string = "GET";
let _url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() +
"/Programs/:id/findTeamRoles";
let _routeParams: any = {
  id: id
};
let _postBody: any = {};
let _urlParams: any = {};
if (userId) _urlParams.userId = userId;
let result = this.request(_method, _url, _routeParams, _urlParams, _postBody);
return result.map((instances: Array<Program>) =>
    instances.map((instance: Program) => new Program(instance))
);
}

我收到此错误:

 core.es5.js:1084 ERROR TypeError: instances.map is not a function

2 个答案:

答案 0 :(得分:4)

该错误只有一种可能性。 Map是数组的原型函数,因此instancesnullundefined

答案 1 :(得分:0)

我在打字稿文件中遇到此错误,我在那儿从服务中提取了一些数据。而且我知道mapArray的{​​{3}}。所以我只是看代码,发现函数中缺少一个await关键字。这是一个小错误。

之前

public async getAddressesBySupplierId(supplierId: string): Promise<IAddressData[]> {
        const addresses: IAddressData[] = this.spFilter.getItems({
            title: config.listNames.adress
        });
        const addressWithType = addresses.map(async (address) => {
        const addressType = await this.getAddressTypeById(address.AdressartIdId);
        return await this.mapAddressType(address, addressType);
    });
        return Promise.all(addressWithType);
    }

之后

public async getAddressesBySupplierId(supplierId: string): Promise<IAddressData[]> {
        const addresses: IAddressData[] = await this.spFilter.getItems({
            title: config.listNames.adress
        });
        const addressWithType = addresses.map(async (address) => {
        const addressType = await this.getAddressTypeById(address.AdressartIdId);
        return await this.mapAddressType(address, addressType);
    });
        return Promise.all(addressWithType);
    }

如您所见,在第一个代码块中,我在第二行(在async调用之前)缺少this.spFilter.getItems关键字。