0

I'm trying to build a simple report server with node express. But, it is not functioning as expected.

This is my api end point to generate report:

router.post('/test', async function (req, res) {
  return res.json(await reportService.test());
});

This is service layer:

var pdf = require("pdf-creator-node");
var fs = require("fs");
var path = require("path");
var base64Util = require("../utils/base64util");

async function test() {

  var html = fs.readFileSync(path.join(__dirname, "../templates/test.html"), "utf8");

  var options = {
    format: "A3",
    orientation: "portrait",
    border: "10mm",
    header: {
      height: "45mm",
      contents: '<div style="text-align: center;">header</div>'
    },
    footer: {
      height: "28mm",
      contents: {
        first: 'Cover page',
        2: 'Second page',
        default: '<span style="color: #444;">{{page}}</span>/<span>{{pages}}</span>',
        last: 'Last Page'
      }
    }
  };

  var users = [{
      name: "Shyam",
      age: "26",
    },
    {
      name: "Navjot",
      age: "26",
    },
    {
      name: "Vitthal",
      age: "26",
    },
  ];

  var document = {
    html: html,
    data: {
      users: users
    },
    path: path.join(__dirname, "../reports/test.pdf"),
    type: "",
  };

  pdf
    .create(document, options)
    .then(async (res) => {
      logger.info("Report Generated: " + res.filename);
      let base64 = await base64Util.convert("../reports/test.pdf");

      return {
        success: true,
        url: res.filename,
        base64: base64
      };
    })
    .catch((error) => {
      logger.error(error);

      return {
        success: false
      };
    });
}

module.exports = {
  test
}

This is pdf to base64 converting method:

const pdf2base64 = require('pdf-to-base64');
var path = require("path");

async function convert(filePath){
pdf2base64(path.join(__dirname, filePath))
.then(
    (response) => {
        return response;
    }
)
.catch(
    (error) => {
        logger.log(error);
        return false;
    }
)}

module.exports = {
 convert
}

I want send the response after generating the pdf and then converting it to base64. But it didn't return anything.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • 1
    you are using `async` incorrectly with `convert`, there is no `return` value. same for `test` function. also `.then(response => { return response })` doesn't do anything. `async function convert(filePath) { return await pdf2base64(...) }` actually returns a value but is an anti-pattern. simply write `function convert(filePath) { return pdf2base64(...) }` or simply `const convert = filePath => pdf2base64(...)`. leave error handling for the caller – Mulan Apr 23 '21 at 18:48
  • 1
    You have several misunderstandings of `async` and `await`. I would recommend [this Q&A](https://stackoverflow.com/a/66933154/633183) and [this Q&A](https://stackoverflow.com/a/66914501/633183) – Mulan Apr 23 '21 at 19:10
  • 1
    It works! Thank you for help. I should study more abount Async/Await :) – Aruna Ranathunge Apr 24 '21 at 05:50

2 Answers2

0

Your test function needs to return something. Add a return before pdf.create(document...)

user3781737
  • 932
  • 7
  • 17
0

Your test method doesn't wait for anything, because you haven't told it to. Neither it returns anything. Add return in front of your pdf promise:

  return pdf
    .create(document, options)
    ...

The same applies to pdf2base64 call.

function convert(filePath) {
  return pdf2base64(path.join(__dirname, filePath))
    ...
}

Also, you specify those methods to be async, but you aren't actually making any use of async/await promises. You can get rid of them, traditional Promises don't need them.

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise