javascript - 发布请求后出现“发送到客户端后无法设置标头”错误
问题描述
需要帮助解决一些奇怪的错误
这是托管在 heroku 上的 express 的 nodejs 服务
这是我的路由控制器代码
async function PopupConroller(req, res) {
let credential = req.credential;
let zoho_token = null;
console.log('res :>> ', res);
try {
zoho_token = await axios.post(`https://accounts.zoho.com/oauth/v2/token?client_id=${credential.client_id}&client_secret=${credential.client_secret}&refresh_token=${credential.refresh_token}&grant_type=refresh_token`);
console.log('zoho_token.data :>> ', zoho_token.data);
} catch (error) {
console.log('ex 2 :>> ',error);
}
console.log('res :>> ', res);
res.status(200).json({status:"ok"});
return;
}
当服务收到请求时,代码抛出此错误 (来自 axios.post 请求没有错误)
回复:200 OK with no body
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:530:11)
at ServerResponse.header (/app/node_modules/express/lib/response.js:771:10)
at ServerResponse.json (/app/node_modules/express/lib/response.js:264:10)
at PopupConroller (/app/voicenter.js:261:28)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
删除这些行时一切正常响应:200 OK with {status:"ok"}
async function PopupConroller(req, res) {
let credential = req.credential;
let zoho_token = null;
console.log('res1 :>> ', res);
try {
//zoho_token = await axios.post(`https://accounts.zoho.com/oauth/v2/token?client_id=${credential.client_id}&client_secret=${credential.client_secret}&refresh_token=${credential.refresh_token}&grant_type=refresh_token`);
//console.log('zoho_token.data :>> ', zoho_token.data);
} catch (error) {
console.log('ex 2 :>> ',error);
}
console.log('res2 :>> ', res);
res.status(200).json({status:"ok"});
return;
}
在第一个示例中,
当我检查 res 对象 (console.log res1) 时标头未发送,但在第二个示例 (console.log res2) 中,我注意到响应标头已发回。
这条线发送标题的方式???*需要说其他路线中的类似代码可以正常工作
zoho_token = await axios.post(`https://accounts.zoho.com/oauth/v2/token?client_id=${credential.client_id}&client_secret=${credential.client_secret}&refresh_token=${credential.refresh_token}&grant_type=refresh_token`);
感谢您的帮助...谢谢
解决方案
我只是在这里猜测,这是你想要的吗?
要扩展下面的代码段,您会收到错误cannot set headers after they are sent to the client
,因为axios.post()
正在发出HTTP发布请求。
在您的代码中,您通过 axios 进行 HTTP 调用,以便发送标头。
之后,JS 解析器继续评估代码并在 try 块之后,因为没有错误,它评估 res.status() 以获取已发送的响应,因此出现错误。
因此,要解决这个问题,您可以像处理 post 请求一样处理,而无需再次将响应重新发送给客户端。然后,您可以使用“thenable”函数中的承诺响应“做某事”。
如果你想通过 axios 发布一个可选对象,你可以通过将参数传递给发布请求来做到这一点。
async function PopupConroller(req, res) {
let credential = req.credential;
let zoho_token = await axios.post("whatever", {"cat": "meaows"})
.then((response)=> {
// do something with the response
})
.catch((err) => console.log(err))
}
推荐阅读
- operating-system - 是否可以从主机访问虚拟机内存而不对虚拟机中的任何进程可见?
- c++ - 如何比较两个对象?
- python - 使用 random.sample,我如何让它打印相邻的字母
- npm - 在 package.json 中执行 bash 脚本
- amazon-web-services - 当查询超时值较高时,AWS 海王星抛出超时错误
- vba - vb.net如何退出递归循环
- c# - 订阅 Firebase 实时数据库 ChildChange 事件会触发事件,其中子级不是来自订阅路径
- excel - 如何将电子邮件标记为公共与内部?
- python - ImportError:无法从“bson.py3compat”导入名称“abc”
- javascript - npm run build-storybook 失败并显示“模块解析失败:意外令牌 (20:25)”