Javascript 尝试在节点js中的axios in then方法中捕获 在我点击下面的api调用后,我得到了错误。 (节点:5548)未处理的PromisejectionWarning:未处理的承诺拒绝(拒绝id:1):错误:发送后无法设置头。 (节点:5548)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。 所以我把try-catch放在了我的then方法中,但仍然无法捕捉到catch中的错误 我通过将consoleconsole.log(“try-catch-error-->”,error)放入进行调试,但仍然没有帮助 你能让我知道我是否在我的then方法中添加了正确的try和catch 下面提供我的代码片段 axios.get(AppConstants.get\u JWT\u TOKEN\u URL{ 认证:{ 用户名:credentials.auth.racfId,密码:credentials.auth.password } }) .然后((jwtResponse)=>{ log(“jwtthen-->”,jwtResponse.data.jwt); var jwtToken=`Bearer${jwtResponse.data.jwt}`; //var jwtToken=`Bearer-ewefwehjefwwe-wehwefwef-uih-uihu`; log('然后是formatUrl-->',formatUrl); 获取(格式化URL{ 标题:{ “授权”:jwtToken,“内容类型”:“应用程序/json” } }) 。然后((响应)=>{ 试一试{ log(“sports Success then0-->”); const file=Buffer.from(response.data.content,'base64'); const fileType=mime.contentType(response.data.contentInfo.fileType); const fileExtension=response.data.contentInfo.fileType.toLowerCase(); const fileName=`fileName=${response.data.contentInfo.id}.${fileExtension}`; log(“运动成功文件名-->”,文件名); callService(res,url); res.send({}); } 捕获(错误){ log(“try catch error-->”,error) const errorMessage=error.response.data.message; } }) .catch((e)=>{ log(“e catch sports0-->”,e); console.log(“e.message.0-->”,e.message); console.log(“catch sports-->”,e.response); 如果(如回应){ 返回res.status(e.response.status)。发送(e.response.data); } res.status(500)。send(例如,message | | |'有什么不对劲'); }); });

Javascript 尝试在节点js中的axios in then方法中捕获 在我点击下面的api调用后,我得到了错误。 (节点:5548)未处理的PromisejectionWarning:未处理的承诺拒绝(拒绝id:1):错误:发送后无法设置头。 (节点:5548)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。 所以我把try-catch放在了我的then方法中,但仍然无法捕捉到catch中的错误 我通过将consoleconsole.log(“try-catch-error-->”,error)放入进行调试,但仍然没有帮助 你能让我知道我是否在我的then方法中添加了正确的try和catch 下面提供我的代码片段 axios.get(AppConstants.get\u JWT\u TOKEN\u URL{ 认证:{ 用户名:credentials.auth.racfId,密码:credentials.auth.password } }) .然后((jwtResponse)=>{ log(“jwtthen-->”,jwtResponse.data.jwt); var jwtToken=`Bearer${jwtResponse.data.jwt}`; //var jwtToken=`Bearer-ewefwehjefwwe-wehwefwef-uih-uihu`; log('然后是formatUrl-->',formatUrl); 获取(格式化URL{ 标题:{ “授权”:jwtToken,“内容类型”:“应用程序/json” } }) 。然后((响应)=>{ 试一试{ log(“sports Success then0-->”); const file=Buffer.from(response.data.content,'base64'); const fileType=mime.contentType(response.data.contentInfo.fileType); const fileExtension=response.data.contentInfo.fileType.toLowerCase(); const fileName=`fileName=${response.data.contentInfo.id}.${fileExtension}`; log(“运动成功文件名-->”,文件名); callService(res,url); res.send({}); } 捕获(错误){ log(“try catch error-->”,error) const errorMessage=error.response.data.message; } }) .catch((e)=>{ log(“e catch sports0-->”,e); console.log(“e.message.0-->”,e.message); console.log(“catch sports-->”,e.response); 如果(如回应){ 返回res.status(e.response.status)。发送(e.response.data); } res.status(500)。send(例如,message | | |'有什么不对劲'); }); });,javascript,node.js,api,express,Javascript,Node.js,Api,Express,日志 运动成功0---> 运动成功文件名-->ioreioreio=ERIERIIOREIO callService===>/erpoper/rejklerkller 如果responseutil.jsURL==>http://players/erpoperoperop/rejklerklkler URL==>http://players/erpoperoperop/rejklerklkler express deprecated res.send(status,body):使用res.stat

日志

运动成功0--->
运动成功文件名-->ioreioreio=ERIERIIOREIO
callService===>/erpoper/rejklerkller
如果responseutil.jsURL==>http://players/erpoperoperop/rejklerklkler
URL==>http://players/erpoperoperop/rejklerklkler
express deprecated res.send(status,body):使用res.status(status).send(body)代替server\services\utils\ResponseUtil.js:56:30
(节点:5548)未处理的PromisejectionWarning:未处理的承诺拒绝(拒绝id:1):错误:发送后无法设置头。
(节点:5548)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。

此实现存在几个问题。第一个问题是,您忽略了回报您的承诺,以便将其应用于承诺链。如果您不返回承诺,那么错误将不会在承诺链上传播,这违背了承诺的目的。第二个问题是,您试图发送两次响应,一次在
ResponseUtil.callService(res,url)
中,另一次直接发送(例如
res.send({});

控制台错误指出了这两个错误:

1) 未能兑现承诺

(节点:5548)未处理的PromisejectionWarning:未处理的承诺拒绝(拒绝id:1):错误:发送后无法设置头

2) 重复呼叫
res.send

express deprecated res.send(status,body):使用res.status(status).send(body)代替server\services\utils\ResponseUtil.js:56:30


我将假设
ResponseUtil.callService(res,url)
返回一个承诺来回答这个问题,因为情况似乎就是这样

axios.get(AppConstants.GET_JWT_TOKEN_URL, {
    auth: {
        username: credentials.auth.racfId,
        password: credentials.auth.password
    }
}).then((jwtResponse) => {
    // Return the promise from get so it is applied to the promise chain
    return axios.get(formatUrl, {
        headers: {
            "Authorization": `Bearer ${jwtResponse.data.jwt}`,
            "Content-Type": 'application/json'

        }
    }).then((response) => {
        const file = Buffer.from(response.data.content, 'base64');
        const fileType = mime.contentType(response.data.contentInfo.fileType);
        const fileExtension = response.data.contentInfo.fileType.toLowerCase();
        const fileName = `filename=${response.data.contentInfo.id}.${fileExtension}`;
        // Return the promise from call service so it is applied to the promise chain
        return ResponseUtil.callService(res, url);
    });
}).catch((e) => {
    // Catch any error that occurred in the promise chain...
    if (e.response) {
        return res.status(e.response.status).send(e.response.data);
    }
    return res.status(500).send(e.message || 'Something wrong');
});

你能重新措辞这个问题吗?很难理解你在问什么。@shaochuancs hey抱歉在我发布之前看到了这个问题…我正在尝试实现try-catch isnide axios then方法:(感谢那个错误消失了…我更新了我的问题。你能告诉我如何删除负面标记吗?(如果你删除这行
res.send({})
如何将数据发送到浏览器?因为在浏览器中我需要下载pdf
响应,IL
正在发送响应。错误消息说不要使用
res.send(状态,正文)
并且它来自
ResponseUtil
。不幸的是,您无法删除其他用户给出的负面标记,他们可能会给您负面标记,因为您的帖子最初的格式不正确。为了避免以后出现这种情况,请在发布之前确保您的问题的格式正确。您当然可以删除您的问题,然后is将从您的帐户中删除负面标记。但是给出了很好的解释这将帮助其他人…是否仍然可以删除负面标记而不删除:(您可以尝试进一步改进您的问题的格式,以使其更具可读性。如果您改进,每个人可能都不清楚您在问什么