I have following API list below:
1. https://www.something.com/?filter=something
Result:
{
id: 12
previous: null
next: https://www.something.com/?filter=something&page=2
data: ...
}
2. https://www.something.com/?filter=something&page=2
Result:
{
id: 13
previous: https://www.something.com/?filter=something
next: https://www.something.com/?filter=something&page=3
data: ...
}
3. https://www.something.com/?filter=something&page=3
Result:
{
id: 14
previous: https://www.something.com/?filter=something&page=2
next: null
data: ...
}
I want to loop through all of the urls after getting the data back of each API call, check if the next variable is null. If it is not null, it will keep continue going. while it's going through, the data of each API call will be concat and return to output.
This is what i have been trying so far
getByURL(url: string): any {
return this.apiService.get(url).pipe(map(data => data));
}
getData(): Observable<any> {
const url = 'https://www.something.com/?filter=something';
return this.getByURL(url).pipe(
expand(({ data }) => {
if (!data.next) return this.getByURL(data.next);
else return empty();
}),
concatMap(({ content }) => {
return of(content.data);
})
);
}
But i got undefined result, i have struggled it for several day. Thanks
UPDATED
I put my code here in stackblitz: I have 3 api call :
http://5cf15627c936cb001450bd0a.mockapi.io/users
http://5cf15627c936cb001450bd0a.mockapi.io/users2
http://5cf15627c936cb001450bd0a.mockapi.io/users3
I want to gather all of the data after each requests. Please see the console to see the output. i always get undefined. Thanks
https://stackblitz.com/edit/angular-rai6az?file=src%2Fapp%2Fapp.component.ts