Problem: When I fetch data from the server I get some object in the format:
{
list: {
pagination: {},
entries: []
}
}
I need to go through entries and change them. To change them I need to call some REST API for each entry (for example: in the first call I will get ids, and for each id, I need to get real data). After all REST calls are finished for each object I need to change its data. so entries[i] = from id to real data.
I resolved the problem with Promises but I want to do that with Observables.
I tried to use map and concatMap from rxjs but without success.
Code:
public getUsers(queryBody?: SearchRequest): Observable<PersonPaging> {
const searchRequest =
queryBody ||
this.searchService.makeSearchRequest(
new Query({ type: ContentModel.TYPE_PERSON })
);
return this.searchService
.search(searchRequest)
.pipe(
switchMap(async (resultSetPaging: ResultSetPaging) => {
const users: PersonPaging = {
list: {
pagination: resultSetPaging.list.pagination,
entries: []
}
};
for (const rowEntry of resultSetPaging.list.entries) {
const { properties } = rowEntry.entry;
const personEntry = await this.getUser(
properties[ContentModel.PROP_USERNAME]
).toPromise();
const person: Person = personEntry.entry;
person.avatarId = this.getUserAvatarImage(person);
users.list.entries.push(personEntry);
}
return users;
})
);
}
END OF QUESTION
CODE IN PROGRESS:
return this.searchService.search(searchRequest).pipe(
map((resultSetPaging: ResultSetPaging) => {
return from(resultSetPaging.list.entries).pipe(
concatMap(rowEntry => {
const { properties } = rowEntry.entry;
return from(
this.getUser(properties[ContentModel.PROP_USERNAME])
).pipe(
map(personEntry => {
const person: Person = personEntry.entry;
person.avatarId = this.getUserAvatarImage(person);
return { person };
})
);
})
);
})
);
With this code, I get all users with modified data but I don't get the object that I want with pagination. And it's call x times (x - number of users/entries)