【问题标题】:Http rxjs - Passing Subscribe result to another functionHttp rxjs - 将订阅结果传递给另一个函数
【发布时间】:2018-07-09 02:27:34
【问题描述】:

请您指导我正确的方向吗?

我正在尝试从旧的 Http REST 服务中提取记录,重组数据,然后将其发布到 firebase。

我不确定这是完全错误的方法还是我完全误解了 observable 的方法。您的指导将不胜感激。如何将此新列表发送到另一个 DataService 函数:exportToFirebase(new_listing)?

onServiceCall() {
  this.httpDataService.getData().subscribe((itemData) => {
    this.itemDataJSON = itemData;
    if (this.itemDataJSON) {
      this.itemDataJSON.forEach(function(value) {
        let new_listing = new Listing(value.id, value.name, value.category);
        //console.log(new_listing);
        //How to send this new listing to another firebaseDataService function exportToFirebase(new_listing);
      });
    }
  });
}

【问题讨论】:

  • 您不能“发送”数据,但另一方面您可以拉取数据。因此,不要从onServiceCall() 发送它,而是在您的DataService 上调用this.httpDataService.getData(),然后使用.switchMap.map 对结果做任何你想做的事情。
  • exportToFirebase 的函数签名是什么
  • exportToFirebase 返回什么?

标签: http rxjs httpclient angular6 subscribe


【解决方案1】:

这个 demo 使用模拟的 http 和 firebase 服务做你想做的事。

// --- make http call
httpCall.pipe(
  // --- construct an array of "new_listing" (of just one, up to u) and pass along the observable chain
  concatMap(httpNumbers => of(...httpNumbers)),
  // --- make the firebase call
  concatMap(number => firebaseCall(number))
).subscribe(val => console.log(val))

希望这会有所帮助。如果这没有意义,那么您需要阅读RxJS

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    相关资源
    最近更新 更多