问题
I am trying to generate a report using node-jasper package in node.js. When i write the code without class implementation then the code works fine following is the workable code.
jasper = require('node-jasper')({
path: '../lib/jasperreports-5.6.0',
reports: {
hw: {
jasper: '../jasperFiles/NodeIreportTest.jasper'
}
},
drivers: {
mysql: {
path: '../lib/com.mysql.jdbc_5.1.5.jar',
class: 'com.mysql.jdbc.Driver', //odbc driver//sun.jdbc.odbc.JdbcOdbcDriver //mysqlDriver// com.mysql.jdbc.Driver
type: 'mysql'
}
},
conns: {
dbserver1: {
host: '192.168.1.164',
port: 3306,
dbname: 'dbname',
user: 'user',
pass: '1234',
driver: 'mysql'
}
},
defaultConn: 'dbserver1'
});
Following is complete object found when i debug the code. which is having (hm) property.
but when i tried to introduce the classes and make the code standard i get and error TypeError: self.hm is not a constructor
following is class implementation of the same code
class ReportModel {
constructor(strJasperPath, objDataObject, intClientIP) {
this.strJasperPath = strJasperPath;
this.objDataObject = objDataObject;
this.intClientIP = intClientIP;
}
makeReport() {
return new Promise ((resolve, reject) => {
jasper = require('node-jasper')({
path: '../lib/jasperreports-5.6.0',
reports: {
hw: {
//jasper: '../jasperFiles/NodeIreportTest.jasper'
jasper: '../jasperFiles/' + this.strJasperPath + '.jasper' //path to report
}
},
drivers: {
mysql: {
path: '../lib/com.mysql.jdbc_5.1.5.jar',
class: 'com.mysql.jdbc.Driver', //odbc driver//sun.jdbc.odbc.JdbcOdbcDriver //mysqlDriver// com.mysql.jdbc.Driver
type: 'mysql'
}
},
conns: {
dbserver1: {
host: '192.168.1.164',
port: 3306,
dbname: 'dbname',
user: 'user',
pass: '1234',
driver: 'mysql'
}
},
defaultConn: 'dbserver1'
});
var report = {
report: 'hw',
data: this.objDataObject
}
var pdf = jasper.pdf(report);
var strReportName = this.strJasperPath + intClientIP + '.pdf'
var filepath = "./reports/" + strReportName
var filename = {
"filepath": serverConfig.reporthost + path.basename(filepath)
};
//check if file is present or not
fs.access(filepath, fs.constants.F_OK | fs.constants.W_OK, (err) => {
if (err) {
//file is not present
fs.writeFile("./reports/" + strReportName, new Buffer(pdf, 'binary'), (err) => {
if (err) {
reject(err);
}
resolve(filename);
});
} else {
//file is present then delete it and then create it
fs.unlink(filepath, (err) => {
if (err) {
reject(err);
} else {
fs.writeFile("./reports/" + strReportName, new Buffer(pdf, 'binary'), (err) => {
if (err) {
console.log('WriteFile', err)
}
resolve(filename);
});
}
});
}
});
});
}
}
following is the object which is not having hm property with class implementation.
回答1:
Step 1) Remove your Jasper declaration out of the class body and remove hw object from it like below code
jasper = require('node-jasper')({
path: '../lib/jasperreports-5.6.0',
reports: {
// hw removed from this place
},
drivers: {
mysql: {
path: '../lib/com.mysql.jdbc_5.1.5.jar',
class: 'com.mysql.jdbc.Driver', //odbc driver//sun.jdbc.odbc.JdbcOdbcDriver //mysqlDriver// com.mysql.jdbc.Driver
type: 'mysql'
}
},
conns: {
dbserver1: {
host: '192.168.1.164',
port: 3306,
dbname: 'node_incrency_minpcs',
user: 'RAHUL',
pass: '1234',
driver: 'mysql'
}
},
defaultConn: 'dbserver1'
});
Step 2) Add declaration of hw object in constructor like below
constructor(strReportName) {
jasper.reports = {
hw: {
jasper: '../jasperFiles/' + strReportName+ '.jasper'
}
}
}
Step 3) add other parameters to function body that is makeReport
function as makeReport(strJasperPath, objDataObject, intClientIP)
Hope this will work for you.
来源:https://stackoverflow.com/questions/54489985/report-generation-in-node-js-using-node-jasper-package