0

I'm new at field. I trying to make pdf generator web application with express.js using html-pdf package. I have condition on engagement variable

module.exports =
({rs,periode,engagement,siren,num,rue,codePostal,ville,contratRef,commentaire,nomPrenom,fonction,phone,mail}) => {
const today = new Date();
return `
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <script>



    window.onload = function test() {


      var o = (document.getElementById('choix1'));
      var u = (document.getElementById('choix2'));


      if (${ engagement } == "oui") {
        o.checked = true;
      }else if (${ engagement } == "non") {
        u.checked = true
      }
    }


  </script>

When client send data to back end the app crashed and error "cannot set headers after they are sent to the client" in my terminal. how can I resolve this problem ?

Server API code:

app.post('/pdf', (req, res) => {
  pdf.create(pdfDocument(req.body), {}).toFile('result.pdf', (err) => {
    if(err) { res.send(Promise.reject()); }
    res.send(Promise.resolve());
  });
});

Manoj
  • 2,059
  • 3
  • 12
  • 24
  • It looks like issue in server side code. Can you share request handler code for the API being called? – Raeesaa Feb 02 '21 at 09:05
  • app.post('/pdf', (req, res) => { pdf.create(pdfDocument(req.body), {}).toFile('result.pdf', (err) => { if(err) { res.send(Promise.reject()); } res.send(Promise.resolve()); }); }); – Khalil Ayari Feb 02 '21 at 09:12

1 Answers1

0

We normally get cannot set headers after they are sent to the client if response is returned twice or if headers are set after response is returned. You can read more about it in following stack overflow post:

Error: Can't set headers after they are sent to the client

If response object is not being updated from any other middleware apart from request handler code shared, there are chances that pdf.create returned error and res.send got called twice -

  1. within if block for error and
  2. res.send statement after if

If you add return before res.send like below, issue should be resolved

app.post('/pdf', (req, res) => {
  pdf.create(pdfDocument(req.body), {}).toFile('result.pdf', (err) => {
    if(err) {
      return res.send(`Error in generating PDF`);
    }
    return res.send(`PDF created successfully!`);
  });
});

Also, I am not sure why you are returning Promise.resolve() / Promise.reject() in response.

Raeesaa
  • 3,267
  • 2
  • 22
  • 44
  • (node:10004) UnhandledPromiseRejectionWarning: undefined (node:10004) UnhandledPromiseRejectionWarning: Unhandled promise rejection. – Khalil Ayari Feb 02 '21 at 10:56
  • That is because you are returning `Promise.reject()` in response. I am not sure why you are doing that (mentioned same thing in last line of answer as well). You will have to return proper response for this to be fixed. – Raeesaa Feb 02 '21 at 11:00
  • i'm not sure why i did that because i follow someone in tutorial video to do this web application i'm new at this field like i said from the begin – Khalil Ayari Feb 02 '21 at 13:39
  • For now, I will update answer to return string in response so that exception is gone. You will have to update it as per API requirements. – Raeesaa Feb 02 '21 at 13:55
  • the app doesn't crash and also doesn't work.the variable do not arrive to backend – Khalil Ayari Feb 02 '21 at 14:25
  • I can only help till here, you will have to debug it further to figure out the issue. Maybe start by adding console logs in `if(err)` block. – Raeesaa Feb 03 '21 at 07:16