Mysql nodejs中生成的pdf报告中未打印数据
我正在尝试使用api在nodejs中生成一个非常简单的报告。数据来自mysql数据库。但是,生成的pdf文件中没有打印数据。尽管正在打印页眉和页脚。似乎。数据(结果)未被执行。任何建议都将不胜感激。控制台中没有生成错误。代码如下:Mysql nodejs中生成的pdf报告中未打印数据,mysql,node.js,Mysql,Node.js,我正在尝试使用api在nodejs中生成一个非常简单的报告。数据来自mysql数据库。但是,生成的pdf文件中没有打印数据。尽管正在打印页眉和页脚。似乎。数据(结果)未被执行。任何建议都将不胜感激。控制台中没有生成错误。代码如下: app.post('/adminreport', function(req,res){ var reportDate = req.body.report_Date; console.log("Report Date : " + reportDate);
app.post('/adminreport', function(req,res){
var reportDate = req.body.report_Date;
console.log("Report Date : " + reportDate);
connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
connection.query('SELECT id, fName, lName, pickUpDate FROM reservations_db WHERE pickUpDate = ?', [reportDate], function(err, results, fields){
if (!err)
{
//res.sendStatus(200).json({status:"ok"});
console.log('The result is generated successfully!');
console.log(results);
//console.log(fields);
var headerFunction = function(Report) {
Report.print("Report By Date", {fontSize: 22, bold: true, underline:true, align: "center"});
Report.newLine(2);
};
var footerFunction = function(Report) {
Report.line(Report.currentX(), Report.maxY()-18, Report.maxX(), Report.maxY()-18);
Report.pageNumber({text: "Page {0} of {1}", footer: true, align: "right"});
Report.print("Report printed: "+ reportDate, {y: Report.maxY()-14, align: "left"});
};
//var res = function(Report) {
// Report.print(results);
//};
var rpt = new Report("report_js.pdf")
.margins(20) // Change the Margins to 20 pixels // Add Data
.pageHeader(headerFunction) // Add a header
.data(results) // Add Data
.pageFooter(footerFunction) // Add a footer
.render();
}
else
{
console.log('Error while performing persistence!');
}
});
这是生成的pdf文件:
以下是console.log:
Report Date : 12/09/2016
Connection established
The result is generated successfully!
[ RowDataPacket {
id: 3,
fName: 'ffree',
lName: 'dgtrh',
pickUpDate: '12/09/2016' } ]
[object Object]
It is working!
好的,实际上我又错过了一个调用.detail()的方法。以下是未来用户的代码:
app.post('/adminreport', function(req,res){
var reportDate = req.body.report_Date;
console.log("Report Date : " + reportDate);
connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
connection.query('SELECT id, fName, lName, pickUpDate FROM reservations_db WHERE pickUpDate = ?',
[reportDate], function(err, results, fields){
if (!err)
{
//res.sendStatus(200).json({status:"ok"});
console.log('The result is generated successfully!');
console.log(results);
//console.log(fields);
var headerFunction = function(Report) {
Report.print("Report By Date", {fontSize: 22, bold: true, underline:true, align: "center"});
Report.newLine(2);
Report.fontBold();
Report.band([
{data: 'ID#', width: 50, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
{data: 'First Name', width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
{data: 'Last Name', width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
{data: 'PickUp Date', width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}}
], {border:1, width: 0, wrap: 1} );
Report.fontNormal();
};
var reportdetail = function ( Report, data ) {
Report.band( [
{data: data.id, width: 50, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
{data: data.fName, width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
{data: data.lName, width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
{data: data.pickUpDate, width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}}
], {border:1, width: 0, wrap: 1} );
};
var footerFunction = function(Report) {
Report.line(Report.currentX(), Report.maxY()-18, Report.maxX(), Report.maxY()-18);
Report.pageNumber({text: "Page {0} of {1}", footer: true, align: "right"});
Report.print("Report printed: "+ reportDate, {y: Report.maxY()-14, align: "left"});
};
//var res = function(Report) {
// Report.print(results);
//};
var rpt = new Report("report_js.pdf")
.margins(20) // Change the Margins to 20 pixels // Add Data
.pageHeader(headerFunction) // Add a header
.data(results) // Add Data
.detail(reportdetail)
.pageFooter(footerFunction) // Add a footer
.render();
}
else
{
console.log('Error while performing persistence!');
}
});
connection.end(function(err) {
console.log('It is working!');
});
//res.redirect('/admin');
});
好的,实际上我又错过了一个调用.detail()的方法。以下是未来用户的代码:
app.post('/adminreport', function(req,res){
var reportDate = req.body.report_Date;
console.log("Report Date : " + reportDate);
connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
connection.query('SELECT id, fName, lName, pickUpDate FROM reservations_db WHERE pickUpDate = ?',
[reportDate], function(err, results, fields){
if (!err)
{
//res.sendStatus(200).json({status:"ok"});
console.log('The result is generated successfully!');
console.log(results);
//console.log(fields);
var headerFunction = function(Report) {
Report.print("Report By Date", {fontSize: 22, bold: true, underline:true, align: "center"});
Report.newLine(2);
Report.fontBold();
Report.band([
{data: 'ID#', width: 50, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
{data: 'First Name', width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
{data: 'Last Name', width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
{data: 'PickUp Date', width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}}
], {border:1, width: 0, wrap: 1} );
Report.fontNormal();
};
var reportdetail = function ( Report, data ) {
Report.band( [
{data: data.id, width: 50, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
{data: data.fName, width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
{data: data.lName, width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
{data: data.pickUpDate, width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}}
], {border:1, width: 0, wrap: 1} );
};
var footerFunction = function(Report) {
Report.line(Report.currentX(), Report.maxY()-18, Report.maxX(), Report.maxY()-18);
Report.pageNumber({text: "Page {0} of {1}", footer: true, align: "right"});
Report.print("Report printed: "+ reportDate, {y: Report.maxY()-14, align: "left"});
};
//var res = function(Report) {
// Report.print(results);
//};
var rpt = new Report("report_js.pdf")
.margins(20) // Change the Margins to 20 pixels // Add Data
.pageHeader(headerFunction) // Add a header
.data(results) // Add Data
.detail(reportdetail)
.pageFooter(footerFunction) // Add a footer
.render();
}
else
{
console.log('Error while performing persistence!');
}
});
connection.end(function(err) {
console.log('It is working!');
});
//res.redirect('/admin');
});
控制台日志显示的是什么?是否有任何错误被输出?知道哪些代码行正在被执行,哪些代码行没有被执行会很有帮助。@therobinkim,似乎。数据(结果)没有被执行。控制台中没有生成错误。我在问题中添加了console.log。有关解决方案,请参见下面的答案。
console.log
显示了什么?是否有任何错误被输出?知道哪些代码行正在被执行,哪些代码行没有被执行会很有帮助。@therobinkim,似乎。数据(结果)没有被执行。控制台中没有生成错误。我在问题中添加了console.log。有关解决方案,请参见下面的答案。