AnswerBun.com

reload page from express using setInterval

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!

2 Answers

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

Add your own answers!

Related Questions

Single tap doesn’t work in combination with double tap

1  Asked on November 28, 2020 by niko-gamulin

     

OpenMP outputs incorrect answers

2  Asked on November 27, 2020 by mohamed-rady

       

Creating a unique short ID from string value

2  Asked on November 27, 2020 by datta

   

How can I send a message to a specific channel?

1  Asked on November 27, 2020 by tuna-ariyurek

   

Variable name change on entire code in Jupyter Notebook

2  Asked on November 27, 2020 by dogukanakgol

   

Why are all the answers for my javascript quiz come back as wrong?

3  Asked on November 26, 2020 by sean-urbassik

 

How to convert multiple columns of dictionaries to a dataframe?

1  Asked on November 26, 2020 by jacob-steenhuysen

       

How to disable pasting in a TextField in Swift?

14  Asked on November 26, 2020 by eliko

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir