【问题标题】:Not able to call http post method无法调用http post方法
【发布时间】:2021-02-16 19:15:40
【问题描述】:

我是 javascript 世界的新手。 httpService.post 永远不会执行。我怎样才能做到这一点?

async createInboundRequest_1(payload: InboundRequestDto) {
  console.log("Request Dto ");
  console.log(payload);
  return this.fetchLookupReferenceData(payload).then(res => {
    console.log("Final Response ");
    console.log(res);
    this.httpService.post(this.api_url, res).pipe(
      map(res_1 => res_1.data),
    )
  });
}

【问题讨论】:

  • “最终响应”是否已注销?
  • 如果你从不在await 里面createInboundRequest_1 为什么要async createInboundRequest_1?另外,return this.fetchLookupReferenceData(payload).then ...您确实意识到您将始终返回解析为 undefined 的 Promise,因为您不会在 .then 中返回任何内容?

标签: javascript node.js typescript promise observable


【解决方案1】:

我的一位同事建议将上面的代码更改为如下所示,它确实有效,但我仍然不确定为什么会这样。

 async createInboundRequest_1(payload: InboundRequestDto) {
    const enrichedRequest = await this.fetchLookupReferenceData(payload);
    const res = this.httpService.post(this.api_url, enrichedRequest).subscribe(res_1 => {
      return res_1.data;
    })
  }

【讨论】:

  • 请求发生在您订阅时。一个实现可能看起来像 createInboundRequest_1(payload).subscribe((response) => ..... 在发布和订阅之间,您可以通过管道处理您的响应来操作它(过滤器、地图等)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 2014-02-10
  • 2019-04-16
  • 1970-01-01
  • 2017-02-17
  • 1970-01-01
  • 2018-11-07
相关资源
最近更新 更多