logo
Tags down

shadow

Angular RxJs - Getting subscriber instead of the required value


By : Tyler Geihlich
Date : July 30 2020, 07:00 AM
I hope this helps . The method subscribe returns a Subscriber. That makes sense right? The whole Observable and JS by nature, is mainly async. You fetch data async and you should somehow wait for it, and use a callback to continue with the returned data. See here for the main thread about this.
In your case that would mean that you will have to use something to make the Observables chain. Good thing there are a bunch of operators, there must be one we can use. In this case, the best operator would be mergeMap or concatMap. However, it's unclear to me why you would need the name of the customer, as you are not passing that to the get customer API. Nevertheless, does this solve your query?
code :
getCustomerOrders(customerName: string): Observable<CustomerOrder[]> {
  return this.getCurrentCustomer(customerName).pipe(
    // here you have your customer object, but what do you want to do with it?
    mergeMap((customer) => this.http.get<CustomerOrder[]>(this.customerOrdersUrl))
  );
}

getCurrentCustomer(name: string): Observable<Customer> {
  const url = this.customerNameUrl;

  return this.http.get<Customer>(url).pipe(
    tap(_ => this.log(`fetched customer name=${name}`, 'success')),
    catchError(this.handleError<Customer>(`getCustomer name=${name}`))
  );
}


Share : facebook icon twitter icon

Reusing code in Angular 2, RxJS -- repetitive code within Subscriber body


By : Rubel Islam
Date : March 29 2020, 07:55 AM
it fixes the issue You can use let() operator to group common functionality of observable chains. It takes observable as input and returns an observable, so you could write:
code :
function updateChart(observable: Observable<any>) {
  return observable
    .map(value => {/* do your thing*/})
 // .filter().switchMap().do() // whatever
    .catch(error => console.log("error"))
}
this.catagoryASubject.let(updateChart).subscribe();
this.catagoryBSubject.let(updateChart).subscribe();
this.catagoryCSubject.let(updateChart).subscribe();
this.A = this.catagoryASubject.let(updateChart);

<div> {{ A |async |json }} </div>

How to nicely throw an error to an Angular RXJS Observable subscriber


By : B.Ramdedovic
Date : March 29 2020, 07:55 AM
Any of those help OK, it looks like I found the answer on this page: https://codingblast.com/rxjs-error-handling/
The key is to use the map and catch functions like so:
code :
public makeRequest(requestParameters) {

return Observable.create(observer => {
let url = 'http://example.com/service/action?a=b';
this.http.get(url,{withCredentials: true})
  .map(data => {
    if(data["success"] !== true)
      throw new Error("Something meaningful");
    return data["payload"];
   })
  .catch(err => {
    return Rx.Observable.throw(err);
   });
  .subscribe(data => {
      observer.next(data);
  },
  err => {
    console.log('error: ' + err);
    observer.error(err);
  });
});
}

Angular 6 RxJS, subscriber not getting value


By : user1661359
Date : March 29 2020, 07:55 AM
I wish this help you As noted in the comments, Angular 6 HttpClient will only accept JSON as a response per default. In order to change the acceptable response type, add a third parameter to the post function call, like so:
code :
return this.http.post('auth/login/', loginModel, {responseType: 'text'})

RXjs emit a value to a subscriber before piping another value to the same subscriber


By : user3607014
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Solution without concat and startsWith - if your want to handle a stream, e.g. false, true, false, false, ..., you will get a false, %mod false%, true, %mod true% ....
code :
Observable.from(false)
  .pipe(
    flatMap(x$ => from([x$, doSomethingWithAValue(x$)])),
  )

Angular RxJS: Make subscriber callbacks run in order


By : Nicolas D
Date : March 29 2020, 07:55 AM
Any of those help Problem: I have to fetch both configuration and data from the server. The data can be rendered only after the configuration is fetched and loaded. , Combine latest will emit once both requests have completed
code :
combineLatest(
    this.getJConfig(),
    this.fetchData()
).subscribe(([config, data]) => {
  // Use config
  // Then use data
});
shadow
Privacy Policy - Terms - Contact Us © voile276.org