Angular:当第一个请求超时时,HTTP Post总是引发第二个请求

时间:2019-03-15 03:36:43

标签: node.js angular post observable

在我的angular.js应用程序中,我有一个按钮用于将数据提交到node.js。但是,如果处理花费的时间太长并超过了超时时间,则node.js中将有另一个触发器并进行第二次处理。我知道Observable具有retry功能,但我没有使用它。

myapp.component.ts

private ngUnsubscribe: Subject<{}> = new Subject();

constructor(private myappService: MyappService) {
}

onSubmit(): void {
    this.myappService.submit()
    .pipe(takeUntil(this.ngUnsubscribe))
    .subscribe(result => {
        // handle result from HTTP Post
    });
}

myapp.service.ts

constructor(private http: HttpClient) {
}

submit(): Observable<any> {
    const url: string = "localhost/api/submit";
    return this.http.post<any>(url, {}).pipe(catchError(/* handle error */));
}

后端(node.js)

exports.submit = (req, res, next) => {
    console.log('process');
    // process something but it exceeds timeout
    console.log('complete');
}

只有1次单击按钮时,这是我的日志。

process   // first and intended processing
process   // second and unintended process start when the first processing is exceed the timeout
complete  // the first processing still run into this code
complete  // the second processing

快速路由器配置-

const router = require('express').Router(); 
const main = require('../controllers/main'); 
router.post('/api/submit', 
        (req, res, next) => { 
        main.submit(req, res, next); 
    });
 module.exports = router;

我想知道自己做错了什么,并使HTTP发布请求触发了两次。

谢谢。

0 个答案:

没有答案