我正在使用AWS Lambda并在Node.js中编写一个方法,当我运行该方法时出现错误
**Cannot read property 'totalCalls' of undefined**
这是我的代码
const mysql = require('mysql');
const moment = require("moment");
var pool = mysql.createPool({
host: process.env.RDS_HOSTNAME,
user: process.env.RDS_USERNAME,
password: process.env.RDS_PASSWORD,
port: process.env.RDS_PORT,
database: process.env.RDS_DATABASE
});
exports.handler = (event, context, callback) => {
console.log('Events:',event);
//let QueueID = event['queueId'];
// allows for using callbacks as finish/error-handlers
context.callbackWaitsForEmptyEventLoop = false;
pool.getConnection(function(err, connection) {
if (err) throw err;
var d = new Date();
let currentDay = moment(d).format('MM-DD-YYYY');
console.log('CurrentDay:',currentDay);
let sub = 6;
d.setDate(d.getDate() - sub);
d.setHours(00);
d.setMinutes(00);
let sql = `select * from ctrData2.CallDetail WHERE InitiationTimestamp > ? ORDER BY InitiationTimestamp ASC`;
//let field = [QueueID];
connection.query(sql,[d], function (err, result, ) {
if (err) throw err;
// connection.release();
var CallaboveSla =0;
var weeklySLAObject={};
result.forEach(d => {
if(weeklySLAObject[d.RoutingProfileName] !== undefined){
console.log('Do i comere here',weeklySLAObject)
weeklySLAObject.RoutingProfileName.totalCalls++;
if(d.AgentInteractionDuration > 180){
if(weeklySLAObject.RoutingProfileName.callsaboveSla){
weeklySLAObject.RoutingProfileName.callsaboveSla++;
}else{
weeklySLAObject.RoutingProfileName.callsaboveSla=0
}
}
}else{
weeklySLAObject[d.RoutingProfileName] = {totalCalls: 0,callsaboveSla:0};
}
// let date = new Date(d.ConnectedToAgentTimestamp);
// let DateNow = moment(date).format('MM-DD-YYYY');
// let datetoday = new Date();
// datetoday.setDate(datetoday.getDate() - 7); //set it to 6 days back
// weeklySLAObject.push(d);
});
let sla = (CallaboveSla/result.length)*100;
callback(null,weeklySLAObject);
});
});
};
我不知道这个错误是什么
在控制台上打印,并且能够检索总计。
值显示在控制台中,但作为响应,它给了我一个错误。
答案 0 :(得分:0)
您正在检查未定义内容:
if(weeklySLAObject[d.RoutingProfileName] !== undefined){
但是您正在访问另一个属性
weeklySLAObject.RoutingProfileName
可能应该更改为
weeklySLAObject[d.RoutingProfileName].totalCalls++