Javascript &引用;发送到客户机后无法设置标题";post请求后出错
需要一些奇怪错误的帮助吗 这是托管在heroku上的express、nodejs服务 这是我的路线控制器代码Javascript &引用;发送到客户机后无法设置标题";post请求后出错,javascript,node.js,express,Javascript,Node.js,Express,需要一些奇怪错误的帮助吗 这是托管在heroku上的express、nodejs服务 这是我的路线控制器代码 async function PopupConroller(req, res) { let credential = req.credential; let zoho_token = null; console.log('res :>> ', res); try { zoho_toke
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没问题,没有尸体
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,状态为{status:“OK”} } 在第一个示例中,
当我检查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`);
感谢你的帮助。。。
谢谢我只是在猜,这就是你想要的吗 要展开下面的代码片段,您会遇到错误
发送到客户端后无法设置头
,因为axios.post()
正在发出HTTP post请求
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))
}
在代码中,通过axios进行HTTP调用,以便发送头
在此之后,JS解析器继续对代码进行求值,在try块之后,由于没有错误,它会对res.status()进行求值,以获得已发送的响应,从而产生错误
因此,要解决这个问题,您可以像处理post请求一样,而无需重新向客户端发送响应。然后,您可以在“thenable”函数中使用承诺响应“做点什么”
如果要通过axios发布可选对象,可以通过将参数传递到post请求中来实现
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))
}
我理解这个错误,但我不明白你想说什么。请你重新表述一下你的问题好吗?在你的/app/voicenter.js文件的第261行到底是什么?此外,您是否在express中设置了可能会启动的错误处理程序?当您收到此错误时,这意味着某些响应已发送回客户端,因此客户端是否收到任何数据?此错误来自某个中间件,通过在解决错误后添加return语句,res.end()之后没有返回。但我仍然不确定这条路线是否有错误,因为我在两条路线上使用了相同的middlwares。尽管如此,谢谢您的回复。@TalWeizman您能详细介绍一下上述评论吗?我不清楚这里的真正问题
PopupController
是一个承诺,它应该使用令牌解析
。您将以状态代码200和json对象结束响应,而事实上,该函数是您应该在任何处理程序调用它时使用的承诺。您可以共享调用PopupController
的处理程序吗?你应该在那里结束回答,而不是在你的承诺中…有什么区别吗?这是一个重复的问题吗?似乎我的评论起了作用,因为你在我写完之后添加了详细的解释;)是的,我意识到用户需要解释错误,所以我添加了它。感谢您错误来自一个中间件,在解决错误后添加了return语句,结果是res.end()没有返回。但我仍然不确定这条路线是否有错误,因为我在两条路线上使用了相同的middlwares。不过,谢谢你的回复。