Chaining a dynamic number of HTTP calls in Angular 7Angular HTML bindingAngular EXCEPTION: No provider for HttpAngular HTTP GET with TypeScript error http.get(…).map is not a function in [null]What is the correct way to share the result of an Angular Http network call in RxJs 5?Angular/RxJs When should I unsubscribe from `Subscription`Huge number of files generated for every Angular projectRX merge multiple observablesAngular 2 - Avoid delay when chaining http requestWhy is my RxJS subscribe error handler not firing?Catching an error and returning of(undefined) stops the stream (Angular 7, rxjs6)
Are glider winch launches rarer in the USA than in the rest of the world? Why?
How can I make sure my players' decisions have consequences?
What is a reasonable time for modern human society to adapt to dungeons?
How to write a sincerely religious protagonist without preaching or affirming or judging their worldview?
Does static fire reduce reliability?
Extrapolation v. Interpolation
How do campaign rallies gain candidates votes?
Keeping an "hot eyeball planet" wet
Impact of throwing away fruit waste on a peak > 3200 m above a glacier
Grid/table with lots of buttons
Historicity doubted by Romans
This message is flooding my syslog, how to find were it comes from?
How did C64 games handle music during gameplay?
Inadvertently nuked my disk permission structure - why?
Memory capability and powers of 2
How can I stop myself from micromanaging other PCs' actions?
Explanation for a joke about a three-legged dog that walks into a bar
Book with a female main character living in a convent who has to fight gods
Moving files accidentally to an not existing directory erases files?
What is the purpose of the fuel shutoff valve?
Very basic singly linked list
Monty Hall Problem with a Fallible Monty
How can I receive packages while in France?
Why are off grid solar setups only 12, 24, 48 VDC?
Chaining a dynamic number of HTTP calls in Angular 7
Angular HTML bindingAngular EXCEPTION: No provider for HttpAngular HTTP GET with TypeScript error http.get(…).map is not a function in [null]What is the correct way to share the result of an Angular Http network call in RxJs 5?Angular/RxJs When should I unsubscribe from `Subscription`Huge number of files generated for every Angular projectRX merge multiple observablesAngular 2 - Avoid delay when chaining http requestWhy is my RxJS subscribe error handler not firing?Catching an error and returning of(undefined) stops the stream (Angular 7, rxjs6)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am developing an application and I encountered some difficulties in the following situation.
I have an url that at its end accepts a pageSize and the current page, and what I am trying to do is using that URL, get in a list all the elements from a database table, without knowing how many pages I actually have (could be one page of data or could be 1 million pages).
For example, after I make a request with lets say page size 50 and page 1, I want to receive page 2, and if page 2 has 50 elements in it, to receive page 3, if it has anything less than 50 elements to stop there and return in the end all the data I received until now, and nothing to be returned until I made a last request.
What I tried to do is the following thing:
return this.http.get(url + currentPage, headers ).pipe(
mergeMap((response: any) =>
managedObject = managedObject.concat(response.managedObjects);
if (response.managedObjects.length === pageSizeNo)
currentPage++;
this.http.get(url + currentPage, headers );
else
return managedObject;
)
);
I initially give the url with pageSizeNo = 50 defined in it and currentPage = 1, then after I receive the response I want to go further and make another call or return everything I received until now. The problem is that everything stops at page 2, and no further call happens.
This is how I subscribe to what the function returns:
this.service.fetchData()
.subscribe((response: any) =>
this.qualityData = response
);
How can I fix this?
angular rxjs
add a comment |
I am developing an application and I encountered some difficulties in the following situation.
I have an url that at its end accepts a pageSize and the current page, and what I am trying to do is using that URL, get in a list all the elements from a database table, without knowing how many pages I actually have (could be one page of data or could be 1 million pages).
For example, after I make a request with lets say page size 50 and page 1, I want to receive page 2, and if page 2 has 50 elements in it, to receive page 3, if it has anything less than 50 elements to stop there and return in the end all the data I received until now, and nothing to be returned until I made a last request.
What I tried to do is the following thing:
return this.http.get(url + currentPage, headers ).pipe(
mergeMap((response: any) =>
managedObject = managedObject.concat(response.managedObjects);
if (response.managedObjects.length === pageSizeNo)
currentPage++;
this.http.get(url + currentPage, headers );
else
return managedObject;
)
);
I initially give the url with pageSizeNo = 50 defined in it and currentPage = 1, then after I receive the response I want to go further and make another call or return everything I received until now. The problem is that everything stops at page 2, and no further call happens.
This is how I subscribe to what the function returns:
this.service.fetchData()
.subscribe((response: any) =>
this.qualityData = response
);
How can I fix this?
angular rxjs
add a comment |
I am developing an application and I encountered some difficulties in the following situation.
I have an url that at its end accepts a pageSize and the current page, and what I am trying to do is using that URL, get in a list all the elements from a database table, without knowing how many pages I actually have (could be one page of data or could be 1 million pages).
For example, after I make a request with lets say page size 50 and page 1, I want to receive page 2, and if page 2 has 50 elements in it, to receive page 3, if it has anything less than 50 elements to stop there and return in the end all the data I received until now, and nothing to be returned until I made a last request.
What I tried to do is the following thing:
return this.http.get(url + currentPage, headers ).pipe(
mergeMap((response: any) =>
managedObject = managedObject.concat(response.managedObjects);
if (response.managedObjects.length === pageSizeNo)
currentPage++;
this.http.get(url + currentPage, headers );
else
return managedObject;
)
);
I initially give the url with pageSizeNo = 50 defined in it and currentPage = 1, then after I receive the response I want to go further and make another call or return everything I received until now. The problem is that everything stops at page 2, and no further call happens.
This is how I subscribe to what the function returns:
this.service.fetchData()
.subscribe((response: any) =>
this.qualityData = response
);
How can I fix this?
angular rxjs
I am developing an application and I encountered some difficulties in the following situation.
I have an url that at its end accepts a pageSize and the current page, and what I am trying to do is using that URL, get in a list all the elements from a database table, without knowing how many pages I actually have (could be one page of data or could be 1 million pages).
For example, after I make a request with lets say page size 50 and page 1, I want to receive page 2, and if page 2 has 50 elements in it, to receive page 3, if it has anything less than 50 elements to stop there and return in the end all the data I received until now, and nothing to be returned until I made a last request.
What I tried to do is the following thing:
return this.http.get(url + currentPage, headers ).pipe(
mergeMap((response: any) =>
managedObject = managedObject.concat(response.managedObjects);
if (response.managedObjects.length === pageSizeNo)
currentPage++;
this.http.get(url + currentPage, headers );
else
return managedObject;
)
);
I initially give the url with pageSizeNo = 50 defined in it and currentPage = 1, then after I receive the response I want to go further and make another call or return everything I received until now. The problem is that everything stops at page 2, and no further call happens.
This is how I subscribe to what the function returns:
this.service.fetchData()
.subscribe((response: any) =>
this.qualityData = response
);
How can I fix this?
angular rxjs
angular rxjs
edited Mar 26 at 16:13
Artyomska
asked Mar 26 at 15:45
ArtyomskaArtyomska
3734 silver badges23 bronze badges
3734 silver badges23 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Use expand
operator to recursively call http-get. E.g.:
return this.http.get(url + currentPage, headers ).pipe(
expand((response: any) =>
managedObject = managedObject.concat(response.managedObjects);
if (managedObject.length === pageSizeNo)
currentPage++;
return this.http.get(url + currentPage, headers );
else
return EMPTY;
),
// combine all emitted values into one array
toArray()
)
Try this expand operator example
NOTE: currentPage
might leak, so be careful with it.
Well, it sort of worked, but still not really there. Right now it makes all the calls but it instead of returning all the lets say 500 datas at once, it always returns the result of every call, when it should combine all the 50 datas from every call and return only when no more calls are made.
– Artyomska
Mar 26 at 16:15
1
@Artyomska, oh yeah, forgot that. Add.toArray()
at the end. I'll update the example
– kos
Mar 26 at 16:17
1
@Artyomska riiight, import it as an operator and pipe it
– kos
Mar 26 at 16:22
1
Everything worked now, and the data is received as it should be. Thank you very much :)
– Artyomska
Mar 26 at 16:40
1
@Artyomska, you're welcome! Once again, be careful with thecurrentPage
thing. If you subscribe twice to that same stream -- it can be buggy. To fix that you might eithershare
output stream or incorporate page number to the stream data itself (as in the linked expand example).
– kos
Mar 26 at 16:48
|
show 1 more comment
function getPage(pageNo: number): Observable< managedObjects: any[] >
return this.http.get(`url?page=$pageNo`, headers );
of([0, false, []] as [number, boolean, any[]]).pipe(
expand(([pageNo, isFinal, allResults]) => getPage(pageNo).pipe(
map(newResults => [
pageNo + 1,
newResults.managedObjects.length < 50,
[...allResults, ...newResults.managedObjects]
] as [number, boolean, any[]])
)),
takeWhile(([pageNo, isFinal, allResults]) => !isFinal, true),
last(),
map(([pageNo, isFinal, allResults]) => allResults)
).subscribe(finalResults =>
// ...
);
See: https://stackblitz.com/edit/rxjs-tyuefr?file=index.ts for a working example
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55361182%2fchaining-a-dynamic-number-of-http-calls-in-angular-7%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use expand
operator to recursively call http-get. E.g.:
return this.http.get(url + currentPage, headers ).pipe(
expand((response: any) =>
managedObject = managedObject.concat(response.managedObjects);
if (managedObject.length === pageSizeNo)
currentPage++;
return this.http.get(url + currentPage, headers );
else
return EMPTY;
),
// combine all emitted values into one array
toArray()
)
Try this expand operator example
NOTE: currentPage
might leak, so be careful with it.
Well, it sort of worked, but still not really there. Right now it makes all the calls but it instead of returning all the lets say 500 datas at once, it always returns the result of every call, when it should combine all the 50 datas from every call and return only when no more calls are made.
– Artyomska
Mar 26 at 16:15
1
@Artyomska, oh yeah, forgot that. Add.toArray()
at the end. I'll update the example
– kos
Mar 26 at 16:17
1
@Artyomska riiight, import it as an operator and pipe it
– kos
Mar 26 at 16:22
1
Everything worked now, and the data is received as it should be. Thank you very much :)
– Artyomska
Mar 26 at 16:40
1
@Artyomska, you're welcome! Once again, be careful with thecurrentPage
thing. If you subscribe twice to that same stream -- it can be buggy. To fix that you might eithershare
output stream or incorporate page number to the stream data itself (as in the linked expand example).
– kos
Mar 26 at 16:48
|
show 1 more comment
Use expand
operator to recursively call http-get. E.g.:
return this.http.get(url + currentPage, headers ).pipe(
expand((response: any) =>
managedObject = managedObject.concat(response.managedObjects);
if (managedObject.length === pageSizeNo)
currentPage++;
return this.http.get(url + currentPage, headers );
else
return EMPTY;
),
// combine all emitted values into one array
toArray()
)
Try this expand operator example
NOTE: currentPage
might leak, so be careful with it.
Well, it sort of worked, but still not really there. Right now it makes all the calls but it instead of returning all the lets say 500 datas at once, it always returns the result of every call, when it should combine all the 50 datas from every call and return only when no more calls are made.
– Artyomska
Mar 26 at 16:15
1
@Artyomska, oh yeah, forgot that. Add.toArray()
at the end. I'll update the example
– kos
Mar 26 at 16:17
1
@Artyomska riiight, import it as an operator and pipe it
– kos
Mar 26 at 16:22
1
Everything worked now, and the data is received as it should be. Thank you very much :)
– Artyomska
Mar 26 at 16:40
1
@Artyomska, you're welcome! Once again, be careful with thecurrentPage
thing. If you subscribe twice to that same stream -- it can be buggy. To fix that you might eithershare
output stream or incorporate page number to the stream data itself (as in the linked expand example).
– kos
Mar 26 at 16:48
|
show 1 more comment
Use expand
operator to recursively call http-get. E.g.:
return this.http.get(url + currentPage, headers ).pipe(
expand((response: any) =>
managedObject = managedObject.concat(response.managedObjects);
if (managedObject.length === pageSizeNo)
currentPage++;
return this.http.get(url + currentPage, headers );
else
return EMPTY;
),
// combine all emitted values into one array
toArray()
)
Try this expand operator example
NOTE: currentPage
might leak, so be careful with it.
Use expand
operator to recursively call http-get. E.g.:
return this.http.get(url + currentPage, headers ).pipe(
expand((response: any) =>
managedObject = managedObject.concat(response.managedObjects);
if (managedObject.length === pageSizeNo)
currentPage++;
return this.http.get(url + currentPage, headers );
else
return EMPTY;
),
// combine all emitted values into one array
toArray()
)
Try this expand operator example
NOTE: currentPage
might leak, so be careful with it.
edited Mar 26 at 16:22
answered Mar 26 at 16:00
koskos
2,1491 gold badge7 silver badges17 bronze badges
2,1491 gold badge7 silver badges17 bronze badges
Well, it sort of worked, but still not really there. Right now it makes all the calls but it instead of returning all the lets say 500 datas at once, it always returns the result of every call, when it should combine all the 50 datas from every call and return only when no more calls are made.
– Artyomska
Mar 26 at 16:15
1
@Artyomska, oh yeah, forgot that. Add.toArray()
at the end. I'll update the example
– kos
Mar 26 at 16:17
1
@Artyomska riiight, import it as an operator and pipe it
– kos
Mar 26 at 16:22
1
Everything worked now, and the data is received as it should be. Thank you very much :)
– Artyomska
Mar 26 at 16:40
1
@Artyomska, you're welcome! Once again, be careful with thecurrentPage
thing. If you subscribe twice to that same stream -- it can be buggy. To fix that you might eithershare
output stream or incorporate page number to the stream data itself (as in the linked expand example).
– kos
Mar 26 at 16:48
|
show 1 more comment
Well, it sort of worked, but still not really there. Right now it makes all the calls but it instead of returning all the lets say 500 datas at once, it always returns the result of every call, when it should combine all the 50 datas from every call and return only when no more calls are made.
– Artyomska
Mar 26 at 16:15
1
@Artyomska, oh yeah, forgot that. Add.toArray()
at the end. I'll update the example
– kos
Mar 26 at 16:17
1
@Artyomska riiight, import it as an operator and pipe it
– kos
Mar 26 at 16:22
1
Everything worked now, and the data is received as it should be. Thank you very much :)
– Artyomska
Mar 26 at 16:40
1
@Artyomska, you're welcome! Once again, be careful with thecurrentPage
thing. If you subscribe twice to that same stream -- it can be buggy. To fix that you might eithershare
output stream or incorporate page number to the stream data itself (as in the linked expand example).
– kos
Mar 26 at 16:48
Well, it sort of worked, but still not really there. Right now it makes all the calls but it instead of returning all the lets say 500 datas at once, it always returns the result of every call, when it should combine all the 50 datas from every call and return only when no more calls are made.
– Artyomska
Mar 26 at 16:15
Well, it sort of worked, but still not really there. Right now it makes all the calls but it instead of returning all the lets say 500 datas at once, it always returns the result of every call, when it should combine all the 50 datas from every call and return only when no more calls are made.
– Artyomska
Mar 26 at 16:15
1
1
@Artyomska, oh yeah, forgot that. Add
.toArray()
at the end. I'll update the example– kos
Mar 26 at 16:17
@Artyomska, oh yeah, forgot that. Add
.toArray()
at the end. I'll update the example– kos
Mar 26 at 16:17
1
1
@Artyomska riiight, import it as an operator and pipe it
– kos
Mar 26 at 16:22
@Artyomska riiight, import it as an operator and pipe it
– kos
Mar 26 at 16:22
1
1
Everything worked now, and the data is received as it should be. Thank you very much :)
– Artyomska
Mar 26 at 16:40
Everything worked now, and the data is received as it should be. Thank you very much :)
– Artyomska
Mar 26 at 16:40
1
1
@Artyomska, you're welcome! Once again, be careful with the
currentPage
thing. If you subscribe twice to that same stream -- it can be buggy. To fix that you might either share
output stream or incorporate page number to the stream data itself (as in the linked expand example).– kos
Mar 26 at 16:48
@Artyomska, you're welcome! Once again, be careful with the
currentPage
thing. If you subscribe twice to that same stream -- it can be buggy. To fix that you might either share
output stream or incorporate page number to the stream data itself (as in the linked expand example).– kos
Mar 26 at 16:48
|
show 1 more comment
function getPage(pageNo: number): Observable< managedObjects: any[] >
return this.http.get(`url?page=$pageNo`, headers );
of([0, false, []] as [number, boolean, any[]]).pipe(
expand(([pageNo, isFinal, allResults]) => getPage(pageNo).pipe(
map(newResults => [
pageNo + 1,
newResults.managedObjects.length < 50,
[...allResults, ...newResults.managedObjects]
] as [number, boolean, any[]])
)),
takeWhile(([pageNo, isFinal, allResults]) => !isFinal, true),
last(),
map(([pageNo, isFinal, allResults]) => allResults)
).subscribe(finalResults =>
// ...
);
See: https://stackblitz.com/edit/rxjs-tyuefr?file=index.ts for a working example
add a comment |
function getPage(pageNo: number): Observable< managedObjects: any[] >
return this.http.get(`url?page=$pageNo`, headers );
of([0, false, []] as [number, boolean, any[]]).pipe(
expand(([pageNo, isFinal, allResults]) => getPage(pageNo).pipe(
map(newResults => [
pageNo + 1,
newResults.managedObjects.length < 50,
[...allResults, ...newResults.managedObjects]
] as [number, boolean, any[]])
)),
takeWhile(([pageNo, isFinal, allResults]) => !isFinal, true),
last(),
map(([pageNo, isFinal, allResults]) => allResults)
).subscribe(finalResults =>
// ...
);
See: https://stackblitz.com/edit/rxjs-tyuefr?file=index.ts for a working example
add a comment |
function getPage(pageNo: number): Observable< managedObjects: any[] >
return this.http.get(`url?page=$pageNo`, headers );
of([0, false, []] as [number, boolean, any[]]).pipe(
expand(([pageNo, isFinal, allResults]) => getPage(pageNo).pipe(
map(newResults => [
pageNo + 1,
newResults.managedObjects.length < 50,
[...allResults, ...newResults.managedObjects]
] as [number, boolean, any[]])
)),
takeWhile(([pageNo, isFinal, allResults]) => !isFinal, true),
last(),
map(([pageNo, isFinal, allResults]) => allResults)
).subscribe(finalResults =>
// ...
);
See: https://stackblitz.com/edit/rxjs-tyuefr?file=index.ts for a working example
function getPage(pageNo: number): Observable< managedObjects: any[] >
return this.http.get(`url?page=$pageNo`, headers );
of([0, false, []] as [number, boolean, any[]]).pipe(
expand(([pageNo, isFinal, allResults]) => getPage(pageNo).pipe(
map(newResults => [
pageNo + 1,
newResults.managedObjects.length < 50,
[...allResults, ...newResults.managedObjects]
] as [number, boolean, any[]])
)),
takeWhile(([pageNo, isFinal, allResults]) => !isFinal, true),
last(),
map(([pageNo, isFinal, allResults]) => allResults)
).subscribe(finalResults =>
// ...
);
See: https://stackblitz.com/edit/rxjs-tyuefr?file=index.ts for a working example
answered Mar 26 at 16:43
Mike JerredMike Jerred
2,8894 gold badges15 silver badges25 bronze badges
2,8894 gold badges15 silver badges25 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55361182%2fchaining-a-dynamic-number-of-http-calls-in-angular-7%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown