如何return mysql data to caller?有些东西有效,其他情况不起作用?
How to return mysql data to caller? Something works, other case don't work?
这是我的数据库对象代码(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时,为什么return值为“未定义”?
我在ITO中添加了如下语句,DBO对象从DB中获取数据成功
console.log("hi:"+JSON.stringify(resultObj));
但是,在RosterManager对象中无法读取数据。
你能帮忙修一下吗?
将查询包装在承诺中。还要避免在代码中添加 require()
。而是将其保留在文件的开头,而不是在代码执行过程中调用它。
此外,如果您要使用这么多 classes,可以考虑使用 typescript 键入所有内容。
在伊藤class:
static getITOList(year, month){
let DBO=require("../utils/dbo.js");
let dboObj=new DBO();
let resultObj={};
return new Promise((resolve, reject) => {
dboObj.getITOList(year,month,(err,resultList)=>{
if (err){
reject(err);
} {
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));
resolve(resultObj);
}
});
});
}
在名册管理器中
constructor(){
let DBO=require("../utils/dbo.js");
let ITO=require("./ITO.js");
const RosterRule = require('./RosterRule');
this.getRosterList=(year,month)=>{
console.log("RosterRule:"+RosterRule);
ITO.getITOList(year, month).then(itoList => {
console.log(itoList);
}).catch(err => {
// handle error
});
}
}
这是我的数据库对象代码(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时,为什么return值为“未定义”?
我在ITO中添加了如下语句,DBO对象从DB中获取数据成功
console.log("hi:"+JSON.stringify(resultObj));
但是,在RosterManager对象中无法读取数据。
你能帮忙修一下吗?
将查询包装在承诺中。还要避免在代码中添加 require()
。而是将其保留在文件的开头,而不是在代码执行过程中调用它。
此外,如果您要使用这么多 classes,可以考虑使用 typescript 键入所有内容。
在伊藤class:
static getITOList(year, month){
let DBO=require("../utils/dbo.js");
let dboObj=new DBO();
let resultObj={};
return new Promise((resolve, reject) => {
dboObj.getITOList(year,month,(err,resultList)=>{
if (err){
reject(err);
} {
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));
resolve(resultObj);
}
});
});
}
在名册管理器中
constructor(){
let DBO=require("../utils/dbo.js");
let ITO=require("./ITO.js");
const RosterRule = require('./RosterRule');
this.getRosterList=(year,month)=>{
console.log("RosterRule:"+RosterRule);
ITO.getITOList(year, month).then(itoList => {
console.log(itoList);
}).catch(err => {
// handle error
});
}
}