如何使用 rxjs Observable 更新 Angular Post 请求中 HttpParams 中的值

发布于 2025-01-16 04:13:22 字数 1077 浏览 1 评论 0原文

我在 Angular 13 中有一个轮询服务,它使用 rxjs 间隔从外部服务器提取数据。 我能够在指定的时间间隔内正常获取数据。 问题是我无法在下次调用之前使用一组新数据更新 httpParams 。参数包含一组用作 SQL 约束的 ID。

  getAllCurrencies(id, code): Observable<any> {
  this.bookingCode = code;
  this.bookingId = id;  
  let url = 'someurl';
  let headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'});
  let body = new HttpParams()
  .set('booking_code', this.bookingCode)
  .set('booking_id', this.bookingId)
  .set('token', this.token); 
  return this.allCurrencies$ = interval(5000).pipe(
  mergeMap(() =>
    this.http.post<any>(this.GLOBAL_URL + url, body, { headers: headers }).pipe(
      map((res) => {
         let result:any = [];
        if(res.data.length > 0){   
          this.testingOne();         
            this.bookingId = res.data[0].id;
            result = res.data;
        }
        return result;
      }))),retry(),share(),takeUntil(this.stopPolling)
  );
 }

我想在返回新值时更新 .set('booking_id', this.bookingId)this.bookingId 的值,然后继续调用。

I have a polling service in Angular 13 which pulls data from an external server using rxjs interval.
I'm able to get the data alright in the specified interval.
The problem is that I'm unable to update the httpParams with a new set of data before the next call. The params contain a set of IDs used as SQL constrainers.

  getAllCurrencies(id, code): Observable<any> {
  this.bookingCode = code;
  this.bookingId = id;  
  let url = 'someurl';
  let headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'});
  let body = new HttpParams()
  .set('booking_code', this.bookingCode)
  .set('booking_id', this.bookingId)
  .set('token', this.token); 
  return this.allCurrencies$ = interval(5000).pipe(
  mergeMap(() =>
    this.http.post<any>(this.GLOBAL_URL + url, body, { headers: headers }).pipe(
      map((res) => {
         let result:any = [];
        if(res.data.length > 0){   
          this.testingOne();         
            this.bookingId = res.data[0].id;
            result = res.data;
        }
        return result;
      }))),retry(),share(),takeUntil(this.stopPolling)
  );
 }

I want to update the value of this.bookingId in .set('booking_id', this.bookingId) when a new value is returned and then continue the call.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪痕残 2025-01-23 04:13:22

尝试像下面这样的东西,它应该可以工作

map((res) => {
  let result:any = [];
  
  if(res.data.length > 0){   
    this.testingOne();         
    this.bookingId = res.data[0].id;

    body = body.set('booking_id', this.bookingId);
    result = res.data;
  }
  return result;
})

Try something like below, it should work

map((res) => {
  let result:any = [];
  
  if(res.data.length > 0){   
    this.testingOne();         
    this.bookingId = res.data[0].id;

    body = body.set('booking_id', this.bookingId);
    result = res.data;
  }
  return result;
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文