Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10844

ngrx showing "Typerror: entities.reduce is not a function" error even when `entities` contains data

I'm trying to use ngrx entity dataservice for the first time

The API call is being made and I'm recieving the expected dataan array of entities is returned

But when ngrx tries to proccess it it gets an error on the reduce method enter image description here

I have checked other similar questions:

But have not found what is causing this.

The response contains an id and is not undefined, how can I solve this ?

Since the error is happening inside ngrx I'm not sure about what part of my code show you to help me address this.

Upvotes: 3

Views: 921

Answers (2)

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10844

This happens when te result contains an array of objects and another data, so the data has to be mapped - http://github.com/ngrx/platform/issues/2434#issuecomment-751560440

Upvotes: 1

Parth Developer
Parth Developer

Reputation: 1857

Make sure you map your response as Array. This will resolve your issue:

Example :

@Injectable()
export class ForReportsDataService extends DefaultDataService<any> {


    constructor(http:HttpClient, httpUrlGenerator: HttpUrlGenerator, private dataService: DataService) {
        super('forReport', http, httpUrlGenerator);
    }

    getAll(): Observable<any> {
        return from(this.dataService.GET("/api/reports").pipe(     // map here
            map(res => res["items"])                               // map here
        ));
    }

}

Upvotes: 1

Related Questions