【发布时间】:2019-09-15 15:34:29
【问题描述】:
我正在使用 Node.js 在 lambda 中为 Amazon Alexa 开发一项技能。
我已经全局声明了变量,并在函数中初始化并在函数外部访问它。但我收到未定义的错误。请帮忙。
var res;
async function classSection(handlerInput){
standard = handlerInput.requestEnvelope.request.intent.slots.Class.value;
section = handlerInput.requestEnvelope.request.intent.slots.Section.value;
var speechOutput = `Starting attendance for class ${standard}, section ${section}. <break time = "3s"/>
I will call the Names of the students, please say Present or Absent to Mark the attendance. <break time = "1s"/> Lets Start. `;
//getting the list of students from database
con.connect(function(err){
if(!err) {
console.log("Database is connected");
} else {
console.log("Error connecting database");
}
});
const sql = `SELECT StudentDetailID,StudentName FROM attendance_system.student_detail where student_detail.Class = ${standard}
and student_detail.Section = '${section}';`;
console.log(sql);
con.query(sql, function (err, result, fields) {
con.end();
if (!err){
console.log(result);
console.log("Table Data : "+result[1].StudentName);
res = result[1].StudentName;
console.log("Speech : "+ speechOutput + res);
//Here in res I get the name of the student.
}
else
console.log('Error while performing Query.');
});
console.log(res);
//here I get undefined error.
return handlerInput.responseBuilder
.speak(speechOutput + res)
.reprompt()
.withSimpleCard('Attendance System',`Class : ${standard} \n Section : ${section}`)
.getResponse();
}
【问题讨论】:
标签: node.js aws-lambda alexa-skills-kit alexa-app