I have the following service:
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { IRecordEvent } from 'app/shared/model/record-event.model';
type EntityArrayResponseType = HttpResponse<IRecordEvent[]>;
@Injectable({ providedIn: 'root' })
export class RecordsService {
public resourceUrl = SERVER_API_URL + 'api/record-events';
constructor(protected http: HttpClient) {}
find(recordId: number): Observable<EntityArrayResponseType> {
return this.http
.get<IRecordEvent[]>(`${this.resourceUrl}/record-id/${recordId}`, { observe: 'response' })
.pipe(map((res: EntityArrayResponseType) => this.convertDateArrayFromServer(res)));
}
protected convertDateArrayFromServer(res: EntityArrayResponseType): EntityArrayResponseType {
if (res.body) {
res.body.forEach((recordEvent: IRecordEvent) => {
recordEvent.eventDate = recordEvent.eventDate ? moment(recordEvent.eventDate) : undefined;
});
}
return res;
}
And the following component (I put ellipsis to simplify code):
import { Component, OnInit } from '@angular/core';
import { IRecord } from 'app/shared/model/record.model';
import { IRecordEvent } from 'app/shared/model/record-event.model';
import { RecordsService } from './records.service';
import { HttpResponse } from '@angular/common/http';
@Component({
...
})
export class DetailledRecordComponent implements OnInit {
recordEvents?: IRecordEvent[] | null;
constructor(protected recordsService: RecordsService) {}
ngOnInit(): void {
this.setRecordEvents();
}
setRecordEvents(): void {
this.recordsService
.find(this.record.id!)
.subscribe((res: HttpResponse<IRecordEvent[]>) => (this.recordEvents = res.body));
}
}
The service retrieves the following data from backend (8 recordEvents with same recordId):
0: {id: 2, eventDate: "2020-07-09", type: "AFF_DT_TRF_ACC_PREV", recordId: 1, recordCode: "D745/012561"}
1: {id: 3, eventDate: "2020-07-08", type: "AFF_DT_RECEV_DEM_PTF_PREV", recordId: 1, recordCode: "D745/012561"}
2: {id: 4, eventDate: "2020-07-08", type: "AFF_DT_ENV_FACT_PREV", recordId: 1, recordCode: "D745/012561"}
3: {id: 5, eventDate: "2020-07-09", type: "AFF_DT_ENV_CV_RACC_PREV", recordId: 1, recordCode: "D745/012561"}
4: {id: 6, eventDate: "2020-07-09", type: "AFF_DT_VAL_DDESC_PREV", recordId: 1, recordCode: "D745/012561"}
5: {id: 7, eventDate: "2020-07-09", type: "AFF_DT_AFFEC_CA_REA", recordId: 1, recordCode: "D745/012561"}
6: {id: 8, eventDate: "2020-07-08", type: "AFF_DT_ENV_PTF_REA", recordId: 1, recordCode: "D745/012561"}
7: {id: 9, eventDate: "2020-07-08", type: "AFF_DT_DMEO_REA", recordId: 1, recordCode: "D745/012561"}
In Google Chrome inspector I can see that it is correctly get from backend:

My problem is that the component does not retrieve this data. I always get undefined:
I used Observables though (Angular 2 component cannot retrieve data from service), I don't understand what I'm missing here.
I noticed something weird: when I debug the interface with Google Chrome's inspector I have this behavior:
1- In the service, the parameter (recordId) is set (with find() method in component) but res is not

2- The component inits this.recordsEvents with undefined
3- Only after these two steps the service gets the objects from backend
Why the data is not retrieved in first step?
