mrtaz
mrtaz

Reputation: 444

.map is not a function in Angular 2 importing map operator doesn't work

Here is my code, I can't figure out why this is saying .map not a function. This is a generic method.

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

I'm getting this error:

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

Upvotes: 1

Views: 4523

Answers (2)

Sibeesh Venu
Sibeesh Venu

Reputation: 21779

I was getting this error in my typescript file, where I was pulling some data from a service. And I knew that the map is a prototype function of the Array. So I was just looking at the code and I found that I was missing an await keyword in my function. It is a small mistake.

Before

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

After

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

As you can see that on the first code block, I was missing the async keyword on the second line (before the this.spFilter.getItems call).

Upvotes: 0

Serginho
Serginho

Reputation: 7490

There is only one possibility for that error. Map is a prototype function of Array, so that instances is null or undefined.

Upvotes: 4

Related Questions