First of all be careful with doing .trim() to a variable that could be undefined. I would change your
if (term.trim()) { ... }
with
if (term && term.trim()) { ... }
Regarding how to iterate the data retrieved from the observable, I would do the following. Suppose that your function getApiData belongs to a service my-backend.service.ts. Suppose that you want to invoke in a component somewere in your code. If you have your service added in your app.modules, you can inject it in your component from the constructor like this:
first import it:
import {MyBackend} from "/.myBackend.service";
then add it to the constructor (inject it) and a global variable for your data:
data:any;
constructor( public myBackend: MyBackend){}
Now wherever you want to invoke your service you do the following:
this.myBackend.getApiData(term).subscribe( (data) => {
this.data = data;
});
And you will have your data stored in your component. Now you can iterate it or do whatever is necesary (data["next"] as you point out for instance).
Let me know if I am unclear or if you need more detail.
EDIT
If you are dealing with a paged search, your this.data should have the info regarding the number of pages or number of results found versus number of results shown. In that case you will need to decide on-the-go if you have to search again and invoke the service several times (or inform the user).
In this case I believe it would not make sense to iterate calls to http.get in your service, paged searches are not intent to work like that, rather the logical option would be to show the results page by page to the user.
If nevertheless you want to do something like that (automatically iterate searches from your component). You could implement a function like this in your component:
async retrieveAllPages(searchInput){
let finish = false;
dataPages = [];
while (!finish){
const page = wait this.myBackend.getApiData(searchInput).toPromise()
dataPages.push(page);
finish = this.decideIfSearchIsFinished(page);
searchInput = this.updateSearchTermNextPage (searchInput);
}
}
You will also need these functions. I do not specify them because I do not know how your backend api works:
decideIfSearchIsFinished(page):boolean{
//looks into the page object response to see if there are more pages or if we are in the last one
//..
}
updateSearchTermNextPage(term):string{
//updates the search term to search for a new page
}