Javascript 未经处理的PromisejectionWarning:错误[ERR_HTTP_HEADERS_SENT]-setInterval&;Axios.post错误处理
我试着通过谷歌搜索来处理这个警告。然而,由于我无法解决这个警告,我提出了这个问题 以下是我现在面临的警告:Javascript 未经处理的PromisejectionWarning:错误[ERR_HTTP_HEADERS_SENT]-setInterval&;Axios.post错误处理,javascript,express,axios,Javascript,Express,Axios,我试着通过谷歌搜索来处理这个警告。然而,由于我无法解决这个警告,我提出了这个问题 以下是我现在面临的警告: (node:39452) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:485:11) at
(node:39452) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:485:11)
at ServerResponse.header
...
at ServerResponse.send
...
at ServerResponse.json
...
at ServerResponse.send
...
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async Timeout.priceAlarm [as _onTimeout]
(node:39452) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
当调用“priceAlarm”时,一切正常。但是,当完成“intervalPriceAlarm=setInterval(priceAlarm,3000,res,chatId);”时,它会在axios.post上显示警告。。。捕捉(错误)零件
有人有好主意来处理吗
多谢各位
function priceAlarm(res, chatId) {
axios
.get(url)
.then((response) => {
currentBtcBitfinexUsd = getPrice(
response.data.tickers,
"BTC",
"USD",
"bitfinex"
);
axios
.post( `${telegramBotUrl}${apiToken}/sendMessage`, {
chat_id: chatId,
text:
"\nBitfinex- BTC(USD):" +
currentBtcBitfinexUsd,
})
.then((response) => {
res.status(200).send(response);
})
.catch((error) => {
res.send(error); //****this part shows an error!*****
});
})
.catch((error) => {
console.log(error);
});
}
这是一个服务器端错误,因为服务器无法重新写入已发送给客户端的响应的HTTP头。因此,您不能将相同的
res
两次发送到res.send()
,但您可以在代码中这样做
//第一次在这里
价格警报(res、chatId)//没问题
//这是第二次
间隔报警(res,chatId);//这里是间隔部分。
您必须重写代码,因为这是HTTP的基本行为,服务器在发送响应后无法向客户端发送数据。所以这不是一个明确的问题,这是一个HTTP限制。如果希望服务器能够将数据“推送”到客户端,则必须使用WebSocket。这是一个服务器端错误,因为服务器无法重新写入已发送到客户端的响应的HTTP头。因此,您不能将相同的
res
两次发送到res.send()
,但您可以在代码中这样做
//第一次在这里
价格警报(res、chatId)//没问题
//这是第二次
间隔报警(res,chatId);//这里是间隔部分。
您必须重写代码,因为这是HTTP的基本行为,服务器在发送响应后无法向客户端发送数据。所以这不是一个明确的问题,这是一个HTTP限制。如果您希望服务器能够将数据“推送”到客户端,则必须改用WebSocket。“服务器在发送响应后无法将数据发送到客户端。”->关于此问题,是否因为“priceAlarm”以相同的信息间隔执行?顺便问一下,为什么它会在“res.send(error);”部分显示错误消息?@ghisooo
intervalAlarm
调用priceAlarm
,它执行res.send
,所以当您在同一中间件处理程序中执行priceAlarm
和intervalAlarm
时,在同一个res
对象上调用res.send
两次,这不会发生。它会以相同的“res”间隔发送响应…顺便问一下,不使用res.send部件可以吗?在“axios.post…然后((响应)=>{res.status(200).send(响应);}).catch((错误)=>{res.send(错误);});”。。好的,这取决于您想做什么“服务器在已经发送响应后无法向客户端发送数据。”->关于这一点,是因为“priceAlarm”以相同的信息间隔执行吗?顺便问一下,为什么它会在“res.send(error);”部分显示错误消息?@ghisooointervalAlarm
调用priceAlarm
,它执行res.send
,所以当您在同一中间件处理程序中执行priceAlarm
和intervalAlarm
时,在同一个res
对象上调用res.send
两次,这不会发生。它会以相同的“res”间隔发送响应…顺便问一下,不使用res.send部件可以吗?在“axios.post…然后((响应)=>{res.status(200).send(响应);}).catch((错误)=>{res.send(错误);});”。。这取决于你想做什么
function intervalAlarm(res,chatId){
if (alarmFlag == 1 && intervalPriceAlarm == null) {
intervalPriceAlarm = setInterval(priceAlarm, 3000, res, chatId);
console.log(intervalPriceAlarm); //need to remove it
}else{
console.log("doing nothing");
}
}
app.post("/", (req,res) =>{
const chatId = req.body.message.chat.id;
const sentMessage = req.body.message.text;
if (sentMessage.match(/price/gi)) {
priceAlarm(res, chatId); //No problem at all
} else if (sentMessage.match(/start/gi)) {
alarmFlag=1;
res.status(200).send({});
} else if(sentMessage.match(/stop/gi)){
alarmFlag=0;
res.status(200).send({});
} else {
res.status(200).send({});
}
intervalAlarm(res,chatId); // here setInterval part.
});