Home > Mobile >  Stop Multiple returns from Server after query with Apollo and Angular
Stop Multiple returns from Server after query with Apollo and Angular

Time:08-24

Bellow is an example on code I am using to submit data to a server and the return I receive and save

this.apollo.mutate( { mutation: XXXXXXXX, variables: { instance_string: X, 
accesstoken: X } })
.subscribe({
   next: (data: any ) => {
     console.log("data returned from Server", data); 
     // This data can be sent from the Server in a loop on occassion
     this.SaveData(data);
   },
   error: (err) => {
     this.presentToastFail(err);
   }
 });

On occasion the Server returns the data in a loop. I have no control of the Server and it appears this will be a reoccurring bug for quite a while. Is there a way that I can can insure the next: only runs once and ignores the Loop of data returns the Server can send.

CodePudding user response:

It looks like you have to use two pipeable operators like take(), filter():

.pipe(
  filter(resp => resp !== undefined && resp !== null),
  take(1) // <--- It will only take one successful valid response
)
.subscribe(...)

CodePudding user response:

It looks like u can also use takeuntil Takeuntil reference document

    const example = evenSource.pipe(
  //also give me the current even number count for display
  withLatestFrom(evenNumberCount),
  map(([val, count]) => `Even number (${count}) : ${val}`),
  //when five even numbers have been emitted, complete source observable
  takeUntil(1)//this will take 1st response
);
  • Related