Heim > Fragen und Antworten > Hauptteil
const db = require('./db');
function Comm_logs(log_id,log_datetime,ip,user_id,machine_name,browser_type,description) {
this.log_id = log_id;
this.log_datetime = log_datetime;
this.ip = ip;
this.user_id = user_id;
this.machine_name = machine_name;
this.browser_type = browser_type;
this.description = description;
}
Comm_logs.create = function (obj) {
if (!obj) {
return null;
}
return new Comm_logs(
obj.log_id,
obj.log_datetime,
obj.ip,
obj.user_id,
obj.machine_name,
obj.browser_type,
obj.description
);
}
Comm_logs.get = ()=> new Promise((resolve,reject)=>{
db
.query(`select log_id,log_datetime,ip,user_id,machine_name,browser_type,description from comm_logs;`)
.then((result)=>{
var comm_logs = [];
for (var i=0; i < result.length; i++) {
comm_logs.push(result[i]);
}
//方法1
resolve(Comm_logs(result));
//方法2
resolve(result);
})
.catch((error)=>{
console.log('Comm_logs.get err');
})
})
module.exports = Comm_logs;
新建一个文件调用之
const comm_logs = require('./comm_logs');
comm_logs
.get()
.then((result)=>{
console.log(typeof(result))
})
.catch((error)=>{
console.log(error);
})
无论方法1还是方法2都是输出object,我目的想输出Comm_logs包装类,请教高玩!