这是我的数据库对象代码(DBO.js):
class DBO
{
constructor(){
let dbConfig = require('./config');
dbConfig["multipleStatements"]=true;
const moment = require('moment');
const mysql = require('mysql2');
const connection = mysql.createConnection(dbConfig);
this.getITOList=(year,month,callBack)=>{
const startDateString=year+"-"+month+"-01";
const endDateString=moment(startDateString).add(1,"M").add(-1,"d").format('YYYY-MM-DD');
let sqlString ="SELECT join_date,leave_date,ito_info.ito_id,post_name,ito_name,available_shift,working_hour_per_day,black_list_pattern from ";
sqlString+="ito_info inner join black_list_pattern ";
sqlString+="on ito_info.ito_id=black_list_pattern.ito_id ";
sqlString+="where join_date<=? and leave_date >=? ";
sqlString+="order by ito_info.ito_id";
console.log("startDateString="+startDateString+",endDateString="+endDateString);
connection.execute(sqlString,[startDateString,endDateString],(err, results, fields)=>{
connection.end(err=>{
if (err) {
throw err;
} else {
callBack(err,results);
console.log("Get ITO List successfully.");
}
});
});
}
this.getRosterRule=(callBack)=>{
let sqlString ="select * from roster_rule order by rule_type,rule_key,rule_value";
connection.execute(sqlString,(err, results, fields)=>{
connection.end(err=>{
if (err) {
throw err;
} else {
callBack(err,results);
console.log("Get Roster Rule successfully!");
}
});
});
}
this.close=()=>{
connection.end(err=>{
if (err) throw err;
console.log("Disconnect form "+process.env["DATABASE_HOST"]+" successfully!");
});
}
}
}
module.exports = DBO;
这是 RosterRule 对象:
class RosterRule{
constructor(){
let DBO=require("../utils/dbo.js");
let dboObj=new DBO();
this.essentialShiftList=null;
this.maxConsecutiveWorkingDay=0;
this.shiftHourCount={};
dboObj.getRosterRule((err,resultList)=>{
if (err){
throw err;
}else {
resultList.forEach(result=>{
switch (result.rule_type){
case 'ConsecutiveWorkingDay':
this.maxConsecutiveWorkingDay=parseInt(result.rule_value);
break;
case 'shiftHour':
this.shiftHourCount[result.rule_key]=parseFloat(result.rule_value);
break;
case 'shiftList':
var temp=result.rule_value.replace(/"/g,'');
this.essentialShiftList=temp.split(',');
break;
}
});
}
});
}
}
module.exports =new RosterRule();
这是一个 ITO 对象。
class ITO
{
constructor(){
/**
* The ITO Id of the specified ITO.
*/
this.itoId="";
/**
* The name of the specified ITO.
*/
this.itoName="";
/**
* The post name of the specified ITO
*/
this.postName="";
/**
* The total no. of working hour per day for the specified ITO.
*/
this.workingHourPerDay=0.0;
/**
* The join date of the specified ITO.
*/
this.joinDate=null;
/**
* The leave date of the specified ITO.
*/
this.leaveDate=null;
/**
* The available shift list of the specified ITO.
*/
this.availableShiftList=[];
/**
* The black listed shift pattern list of the specified ITO.
*/
this.blackListedShiftPatternList=[];
}
static getITOList(year, month){
let DBO=require("../utils/dbo.js");
let dboObj=new DBO();
let resultObj={};
dboObj.getITOList(year,month,(err,resultList)=>{
if (err){
throw err;
}else {
resultList.forEach(ito=>{
let itoObj;
if (resultObj[ito.ito_id]){
itoObj=resultObj[ito.ito_id];
itoObj.blackListedShiftPatternList.push(ito.black_list_pattern);
}else {
itoObj=new ITO();
itoObj.itoId=ito.ito_id;
itoObj.itoName=ito.ito_name;
itoObj.postName=ito.post_name;
itoObj.workingHourPerDay=ito.working_hour_per_day;
itoObj.joinDate=new Date(ito.join_date);
itoObj.leaveDate=new Date(ito.leave_date);
itoObj.availableShiftList=ito.available_shift.split(",");
itoObj.blackListedShiftPatternList.push(ito.black_list_pattern);
}
resultObj[ito.ito_id]=itoObj;
});
console.log("hi:"+JSON.stringify(resultObj));
return resultObj;
}
});
}
}
module.exports = ITO;
最后,这是 RosterManager 对象代码:
class RosterManager
{
constructor(){
let DBO=require("../utils/dbo.js");
let ITO=require("./ITO.js");
const RosterRule = require('./RosterRule');
this.getRosterList=(year,month)=>{
console.log("RosterRule:"+RosterRule);
let itoList=ITO.getITOList(year,month);
console.log(itoList);
}
}
}
module.exports = RosterManager;
所有 RosterRule 属性都可以从数据库成功加载。
当调用 RosterManager.getRosterList 时,为什么返回值是“未定义”?
我在 ITO 中添加了以下语句,DBO 对象已成功从 DB 中获取数据。
console.log("hi:"+JSON.stringify(resultObj));
但是,在 RosterManager 对象中无法读取数据。
你会帮忙修复它吗?