ERR_HTTP_HEADERS_SENT: Cannot set headers after they are sent to the client

前端 未结 5 528
-上瘾入骨i
-上瘾入骨i 2020-12-02 18:41

I\'m facing this weird issue in NodeJS when using with Passport.js, Express and Mongoose. Basically, I get an error saying \"Cannot set headers after they are sent to the cl

5条回答
  •  天涯浪人
    2020-12-02 19:07

    I got the same error using express and mongoose with HBS template engine. I went to Expressjs and read the docs for res.render, and it says // if a callback is specified, the rendered HTML string has to be sent explicitly. So I wasnt originally sending my html explicitly in the callback,. This is only for a contact form btw, not login info, albeit GET

    //Original
    let { username, email } = req.query;  //My get query data easier to read
    
    res.status(200).render('index', { username, email });
    
    
    //Solution without error. Second param sending data to views, Third param callback
    
    res.status(200).render('index', { username, email }, (err, html)=>{
          res.send(html);
     });
    

提交回复
热议问题