Stack Overflow Asked by Dwayne Jackson on October 24, 2020
I have code that
a) handles a POST route,
b) taps API to generate access token and renders a page, and
c) runs the code on interval.
The problem is the first instance works fine, but second instance I get ERR_HTTP_HEADERS_SENT
error. My code:
app.post("/intervalQuote",(req,res)=>{
MongoClient.connect(url,{ useNewUrlParser: true, useUnifiedTopology: true }, (err,db)=>{
if (err) throw err;
const dbo = db.db('ameritrade');
const ticker = req.body.ticker.toUpperCase();
function intervalTimer() {
access_token(dbo).then((access_token)=>{
quote(access_token,ticker).then((quote)=>{
res.render('quote',{result: quote});
})
})
}
intervalTimer();
setInterval(intervalTimer,10*1000);
})
})
I need to be able to run access_token
, then quote
functions before rendering the quote
template. How can I do this on interval and reload the page? Much thanks for the help / suggestion!
Nevermind, I figured it out. Duh, my brain was frozen. All I had to do was move the setInterval function over to client side. Thanks all for looking at the question!
Answered by Dwayne Jackson on October 24, 2020
The error ERR_HTTP_HEADERS_SENT is caused by sending a response several times res.render('quote',{result: quote});
in a setInterval(intervalTimer,10*1000);
. As per de ExpressJS documentation says res.render sends the rendered HTML string to the client.
Once you've sent a response to the client you can't send another response as said here. The res.render
function ends the request-response cycle.
To sum up, don't set an interval to re-render an static HTML.
Answered by Jose Vasquez on October 24, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP