Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
how can i pass value in rxjs subscribe function?

I'm working on switching the code to rxjs

here is my original code.

userAuth$: BehaviorSubject<ArticleInfoRes>;

  async loadArticleList(articleId: number) {
    try {
      const data = await this.articleApi.loadArticleList(articleId);
      this.userAuth$.next(data);
      return data;
    } catch (error) {
      return error;
    }
  }

Subsequently, an attempt was made to convert to rxjs, but the value was not delivered properly. A value or error must be passed to the location where the loadArticleList function is used. Please tell me what's wrong

This is the code I tried to convert.

userAuth$: BehaviorSubject<ArticleInfoRes>;

loadArticleList(articleId: number) {
  from(this.articleApi.loadArticleList(articleId)).subscribe(
    data => {
      this.userAuth$.next(data);
      return data;
    },
    error => {
      return error;
    }
  )
}
about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

A value or error must be passed to the location where the loadArticleList function is used. Please tell me what's wrong

To answer your question directly, the reason why the result (i.e., value or error) isn't passed to where you use loadArticleList() is because your RxJS pipeline is not built properly. For one, adding return data; and return error; in your subscription handlers won't actually return data nor error.

To get the result, you might want to "trickle down" (streamline) that result further down the RxJS pipeline, so that subscription to that result happens in the location you speak of, to where loadArticleList() is used. This translates to moving your call to .subscribe() outside of loadArticleList(), not inside.

So, here's a proper RxJS revision:

import { tap } from "rxjs/operators";

userAuth$: BehaviorSubject<ArticleInfoRes>;

loadArticleList(articleId: number) {
  return from(this.articleApi.loadArticleList(articleId)).pipe(
    tap((data) => this.userAuth$.next(data)) // ๐Ÿ‘ˆ tap into the result to update the next userAuth$
  );
}

Using the tap RxJS operator, we can grab the result of loadAarticleList(articleId), process it according to your callback function inside tap (in this case, simply update the next value of userAuth$), and then with that same grabbed result, unmodified, pass it on to whoever subscribes to loadArticleList().

Finally, to actually "pass" data or errorto the location where the loadArticleList function is used, simply subscribe there.

// in your component's .ts somwehere, probably
loadArticleList(123).subscribe(
  data  => { /* do as you want with the result     */ },
  error => { /* add your error-handling logic here */ }
);
about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!