简体   繁体   English

UnhandledPromiseRejectionWarning: 错误 [ERR_HTTP_HEADERS_SENT] - setInterval & Axios.post 错误处理

[英]UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT] - setInterval & Axios.post error handling

I've tried to handle this warning via Googling.我试图通过谷歌搜索来处理这个警告。 However, as I couldn't solve this warning, I'm asking this question.但是,由于我无法解决此警告,所以我在问这个问题。

The following warning is what I'm facing now:以下警告是我现在面临的:

(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)

When "priceAlarm" is called, it is okay.当调用“priceAlarm”时,没关系。 However, when "intervalPriceAlarm = setInterval(priceAlarm, 3000, res, chatId);"但是,当“intervalPriceAlarm = setInterval(priceAlarm, 3000, res, chatId);”时is done, it shows the warning at axios.post... catch(error) part.完成后,它会在axios.post... catch(error)部分显示警告。

Does anyone have good idea to handle it?有没有人有好主意来处理它?

Thank you.谢谢你。

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);
                  });
} 
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.
});

This is a server-side error due to the fact that the server can't re-write HTTP headers of a response it already sent to the client.这是一个服务器端错误,因为服务器无法重写它已经发送给客户端的响应的 HTTP 标头。 Consequently, you can't res.send() twice the same res , but you do it in your code因此,您不能res.send()两次相同res ,但您可以在代码中执行此操作

// here the first time
priceAlarm(res, chatId); //No problem at all

// here the second time
intervalAlarm(res,chatId);  // here setInterval part.

You'll have to rewrite your code, because that's a base behavior of HTTP that the server can't send data to the client after sending a response already.您必须重写您的代码,因为这是 HTTP 的基本行为,服务器在发送响应后无法向客户端发送数据。 So that's not an express problem, that's an HTTP limitation.所以这不是一个明确的问题,这是一个 HTTP 限制。 If you want your server to be able to "push" data to your client, you'll have to use websockets instead.如果您希望您的服务器能够将数据“推送”到您的客户端,则必须改用 websockets。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (节点:45207)UnhandledPromiseRejectionWarning:错误 [ERR_HTTP_HEADERS_SENT] - (node:45207) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT] (节点:23)UnhandledPromiseRejectionWarning:错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头 - (node:23) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client UnhandledPromiseRejectionWarning:错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头 - UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the clien 我该如何修复(节点:5796)UnhandledPromiseRejectionWarning:错误[ERR_HTTP_HEADERS_SENT]:错误? - How can i fix (node:5796) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: error? 错误 [ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置标头(POST 方法) - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client (POST method) 发布请求:错误 [ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置标头 - Post request: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 数据库更改后出现错误[ERR_HTTP_HEADERS_SENT] - Error [ERR_HTTP_HEADERS_SENT] after change in database 快递和护照错误 ['ERR_HTTP_HEADERS_SENT'] - express and passport error ['ERR_HTTP_HEADERS_SENT'] node.js收到“ ERR_HTTP_HEADERS_SENT”错误 - node.js getting 'ERR_HTTP_HEADERS_SENT' error ERR_HTTP_HEADERS_SENT 错误但找不到它的来源 - ERR_HTTP_HEADERS_SENT Error But Can't Find The Origin of it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM