【发布时间】:2020-04-19 10:53:07
【问题描述】:
我已尝试通过 Google 搜索来处理此警告。但是,由于我无法解决此警告,所以我在问这个问题。
以下警告是我现在面临的:
(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 ... catch(error) 部分显示警告。
有没有人有好主意来处理它?
谢谢。
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.
});
【问题讨论】:
标签: javascript express axios