最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

[node.app.post时未处理的诺言拒绝

网站源码admin20浏览0评论

[node.app.post时未处理的诺言拒绝

[node.app.post时未处理的诺言拒绝

当我的前端收到post请求时

order(market: string, price: string, side: string, size: string): Observable<any> {
        var payload = { market: market, order: { price: price, side: side, size: size } };
        return this.http.post<any>('http://localhost:3000' + "/orders", payload))
            .pipe(
                tap((res: any) => {
                   console.log(res);
                }),
                retry(3), // retry a failed request up to 3 times
                catchError(this.handleError) // then handle the error
            );
    }

我的nodejs服务器收到它

app.post('/orders', (req, res) => {
    console.log(req.body) 
    // more code

    setTimeout(function () {
        axios.post('', payload, { headers: headers })
            .then((response) => { 
                if (response.status === 201) {
                    res.sendStatus(201);                    
                } else {
                    res.sendStatus(400);  
                }           
            })
    }, 500);    
});

我添加了setTimeout,因为没有它我会得到Error: Request failed with status code 400

结果,我可以将订单发布到test/api/orders,但被拒绝承诺。

(node:9220)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。要在未处理的承诺拒绝时终止节点进程,请使用CLI标志--unhandled-rejections=strict(请参阅.html#cli_unhandled_rejections_mode)。 (拒绝ID:2)(节点:9220)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。

我可以在这里进行任何增强吗?

回答如下:

尝试将函数内容移动到try块中并捕获错误。我还建议使用异步等待关键字。

发布评论

评论列表(0)

  1. 暂无评论