[英]Node js Async/Await and return not working as expected
I'm trying to build a simple report server with node express.我正在尝试使用 node express 构建一个简单的报表服务器。 But, it is not functioning as expected.
但是,它没有按预期运行。
This is my api end point to generate report:这是我的 api 端点生成报告:
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:这是 pdf 到 base64 的转换方法:
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.我想在生成 pdf 然后将其转换为 base64 后发送响应。 But it didn't return anything.
但它没有返回任何东西。
Your test
function needs to return something.您的
test
function 需要返回一些东西。 Add a return
before pdf.create(document...)
在
pdf.create(document...)
之前添加一个return
Your test
method doesn't wait for anything, because you haven't told it to.你的
test
方法不会等待任何东西,因为你没有告诉它。 Neither it returns anything.它也不返回任何东西。 Add
return
in front of your pdf
promise:在您的
pdf
promise 前面添加return
:
return pdf
.create(document, options)
...
The same applies to pdf2base64
call.这同样适用于
pdf2base64
调用。
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.此外,您将这些方法指定为
async
,但实际上并没有使用 async/await 承诺。 You can get rid of them, traditional Promises
don't need them.你可以摆脱它们,传统的
Promises
不需要它们。
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await 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 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.