带有Nestjs后端的Angular 9 HTTP调用 [英] Angular 9 http call with nestjs backend

查看:375
本文介绍了带有Nestjs后端的Angular 9 HTTP调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带Nestjs后端Rest api的Angular(9)前端(在nodejs中) 我在后端调用的端点进行了一些相当复杂的操作,大约需要2分钟才能响应. 在前端,我使用subscription来订阅响应,如下所示:

I have an Angular (9) frontend with nestjs backend rest api (in nodejs) The endpoint I call in the backend does some fairly complex operations and it takes about 2 min to respond. In the frontend I use subscribe to subscribe to the response like this:

this.metaService.subscribe(
  result => {
  //Handle result
  },
  error => {
    this.subscription.unsubscribe();
    console.error('Error: ' + error);
  },
  () => {
    this.subscription.unsubscribe();
    console.error('Completed');
  });

问题在于,它在后端开始一遍又一遍地执行代码.请注意,它不会再次执行http调用(我可以从开发人员工具的网络"标签中看到此内容),它始终保持待处理"状态 如果我这样做似乎可以奏效

The problem is that in the backend it starts executing code over and over again. Note that it does not execute the http call again (I can see this from the network tab in Developer tools), it stays in Pending all the time) It seems this works if I do

.pipe(take(1)).subscribe(

然后,它不会在后端执行循环,但会立即完成订阅(并且网络"标签中的http处于取消状态) 我正在Windows机器(Linux子系统)上的本地主机中运行此程序.节点版本为12.14.1

then it does not do the looping in the backend but it immediately completes the subscription (and the http is in cancelled state in network tab) I am running this in my localhost on a windows machine (linux subsystem). Node version is 12.14.1

是否有人导致此问题以及如何解决?

Does anyone what is causing this and how to fix it ?

-Jani

推荐答案

这不是答案:我想我能够弄清楚这一点.我在for循环中有几个静态异步方法,这似乎是造成这种情况的原因.我现在真的不是为什么,但是当我删除方法并将其转换为非静态异步方法(仍处于for循环中)时,我不再遇到此问题. 我不确定为什么它们一开始是静态的...

This is not the answer: I think I was able to figure this out. I had several static async methods in a for loop which seemed to be causing this. I don't really now why but when I removed the methods and converted them to non-static async methods (still in for loop) I am not experiencing the issue anymore. I am not sure why they were static in the first place...

这篇关于带有Nestjs后端的Angular 9 HTTP调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆