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

编程入门 行业动态 更新时间:2024-10-09 23:19:27

[node.app.post时未处理的<a href=https://www.elefans.com/category/jswz/34/1756615.html style=诺言拒绝"/>

[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块中并捕获错误。我还建议使用异步等待关键字。

更多推荐

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

本文发布于:2024-05-07 10:36:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755861.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:诺言   未处理   node   app   post

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!