错误日志
--- buildTaxData ---
[ TaxData {
name: '2017-2018 financial year',
start: '2017-07-01',
end: '2018-06-30',
tax: [ [Object], [Object], [Object], [Object], [Object] ] },
TaxData {
name: '2018-2019 financial year',
start: '2018-07-01',
end: '2019-06-30',
tax: [ [Object], [Object], [Object], [Object], [Object] ] } ]
--- buildTaxData ---
undefined
(node:9166) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'find' of undefined
at Util.getTaxData (/var/www/html/test/testme/myob_payslip_new/lib/Util.js:38:29)
at Employee.buildTaxData (/var/www/html/test/testme/myob_payslip_new/lib/Employee.js:41:28)
at Employee.getIncomeTax (/var/www/html/test/testme/myob_payslip_new/lib/Employee.js:61:18)
at Employee.getNetIncome (/var/www/html/test/testme/myob_payslip_new/lib/Employee.js:72:61)
at EmployeePrint.fillData (/var/www/html/test/testme/myob_payslip_new/lib/EmployeePrint.js:36:24)
at arr.forEach (/var/www/html/test/testme/myob_payslip_new/lib/EmployeePrint.js:55:12)
at Array.forEach (<anonymous>)
at EmployeePrint.print (/var/www/html/test/testme/myob_payslip_new/lib/EmployeePrint.js:53:10)
at Processor.process (/var/www/html/test/testme/myob_payslip_new/lib/Processor.js:19:33)
at App.run (/var/www/html/test/testme/myob_payslip_new/index.js:15:22)
(node:9166) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:9166) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
在EmployeePrint.js中
'use strict';
const Cal = require('./Calculator');
const Util = require('./Util');
const GrossIncome = require('./GrossIncome');
const IncomeTax = require('./IncomeTax');
const NetIncome = require('./NetIncome');
const MySuper = require('./MySuper');
let util = new Util();
let cal = new Cal();
class Employee {
constuctor() {
this.firstName = null;
this.lastName = null;
this.annualSalary = null;
this.superRate = null;
this.timePeriod = null;
}
getFullName() {
let res = this.firstName + ' ' + this.lastName;
return res;
}
getTimePeriod() {
return this.timePeriod;
}
buildTaxData(taxObj) {
// test
console.log('--- buildTaxData ---');
console.log(taxObj);
let startPeriod = util.parseTimePeriod(this.timePeriod)[0] + ' ' + TIME_PERIOD_YEAR;
let endPeriod = util.parseTimePeriod(this.timePeriod)[1] + ' ' + TIME_PERIOD_YEAR;
let currTaxObj = util.getTaxData(taxObj, startPeriod, endPeriod);
let rightTaxSlot = util.getRightTaxSlot(currTaxObj, this.annualSalary);
let base = rightTaxSlot['base'];
let edge = rightTaxSlot['edge'];
let taxRate = rightTaxSlot['rate'];
let myReturn = {
base,
edge,
taxRate
}
return myReturn;
}
getGrossIncome() {
return new GrossIncome(this.annualSalary).calculate();
}
getIncomeTax(taxObj) {
let res = this.buildTaxData(taxObj);
let incomeTax = new IncomeTax(this.annualSalary, res.base, res.edge, res.taxRate);
return incomeTax.calculate();
}
getSuper() {
let mySuper = new MySuper(this.getGrossIncome(), this.superRate);
return mySuper.calculate();
}
getNetIncome() {
let netIncome = new NetIncome(this.getGrossIncome(), this.getIncomeTax());
return netIncome.calculate();
}
fillData(item) {
this.firstName = item.firstName;
this.lastName = item.lastName;
this.annualSalary = item.annualSalary;
this.superRate = cal.percentToNum(item.superRate);
this.timePeriod = item.timePeriod;
return this;
}
}
module.exports = Employee;
在EmployeePrint.js中
'use strict';
const jsonToCSV = require('json-to-csv');
class EmployeePrint {
constructor() {
this.name = null;
this.payPeriod = null;
this.grossIncome = null;
this.incomeTax = null;
this.netIncome = null;
this.mySuper = null;
}
printPromise(data, fileName) {
return new Promise((res, rej) => {
jsonToCSV(data, fileName)
.then(() => {
// success
console.log('Good, print to ' + fileName);
res(true);
})
.catch(error => {
// handle error
console.log('Print error');
rej(true);
});
});
}
fillData(item, taxObj) {
let fullName = item.getFullName();
let timePeriod = item.getTimePeriod();
let grossIncome = item.getGrossIncome();
let incomeTax = item.getIncomeTax(taxObj);
let netIncome = item.getNetIncome();
let mySuper = item.getSuper();
this.name = fullName;
this.payPeriod = timePeriod;
this.grossIncome = grossIncome;
this.incomeTax = incomeTax;
this.netIncome = netIncome;
this.mySuper = mySuper;
return this;
}
async print(arr, fileName, taxObj) {
let buf = [];
arr.forEach((item) => {
let ep = new EmployeePrint();
ep = ep.fillData(item, taxObj).bind(this);
buf.push(ep);
}).bind(this);
return await this.printPromise(buf, fileName);
}
}
module.exports = EmployeePrint;
ep = ep.fillData(item, taxObj).bind(this);
在forEach循环的第二次迭代中,taxObj似乎未定义。是因为循环中存在异步性质吗?如果是这样,我该如何解决?