Report Generation in Node.js Using Node-Jasper Package

女生的网名这么多〃 提交于 2021-02-10 20:18:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!