Javascript Code:
var express = require("express");
var mysql = require('mysql');
var app = express();
var speechwindow,id,intent_id,Intent_response;
function getResults(output,url_id,intent_num,callback) {
// The result for speech window
var speech = output;
console.log(output);
// The result for the plotly embed urls
var url = 'https://plot.ly/~me/'+url_id+'/';
console.log(url);
var img = 'https://plot.ly/~me/'+url_id+'.png';
console.log(img);
var data = 'me:'+url_id;
console.log(data);
// The result for sample utterances
var choose = intent_num;
var Intent= ['RepeatIntent','CustomOptionIntent','HelpIntent','FinishIntent','OpenEndedOptionTwoIntent','OpenEndedOptionOneIntent','ChangeSetpointIntent','ChoiceIntent'];
if (Intent[choose] === 'RepeatIntent'){
var Intent_response = ['repeat the options','can you repeat the options','tell me the options','state the options'];
}
else if (Intent[choose] === 'CustomOptionIntent') {
var Intent_response = ['set the {setpointType} set point to {setPointValue} degree celsius','set the {setpointType} at {setPointValue} degree celsius','{setpointType} is {setPointValue} degrees'];
}
else if (Intent[choose] === 'HelpIntent'){
var Intent_response = ['what questions can I ask','what are various options which I can know about from you','help me','what commands can I say'];
}
else if (Intent[choose] === 'FinishIntent'){
var Intent_response = ['exit','quit','bye','leave'];
}
else if (Intent[choose]=== 'OpenEndedOptionTwoIntent'){
var Intent_response = ['I want to select my own strategy','I want to choose my own strategy','I want to form my custom strategy','I want to form my own strategy'];
}
else if (Intent[choose] === 'OpenEndedOptionOneIntent'){
var Intent_response = ['which is the best strategy to go with','suggest me the best strategy','provide me with best solution for power consumption','suggest me most efficient power solution'];
}
else if(Intent[choose] === 'ChangeSetpointIntent'){
var Intent_response = ['what happens if I change {setpointType} to {setPointValue} percent','what happens if I change {setpointType} to {setPointValue} degrees','what will happen if I change the {setpointType} to {setPointValue} degree celsius','I want to change {setpointType} to {setPointValue} degree celsius'];
}
else if (Intent[choose] === 'ChoiceIntent'){
var Intent_response = ['select option {optionNumber}','select option number {optionNumber}','choose option {optionNumber}'];
}
console.log(Intent_response);
var results = [speech,url,img,data,Intent_response];
}
function generateResponse(req, res) {
var connection = mysql.createConnection({
host : 'localhost',
user : 'user',
password : 'password',
database : 'mydatabase'
});
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... \n\n");
} else {
console.log("Error connecting database ... \n\n");
}
});
connection.query('SELECT * FROM `RTES` ORDER BY ID DESC LIMIT 1', function(err, rows, fields) {
if(!err){
var speechwindow = rows[0].ALEXA;
console.log(speechwindow);
var id = rows[0].URL_ID;
console.log(id);
var intent_id = rows[0].INTENT_NUM;
console.log(intent_id);
}
else{
console.log('Error');}
});
getResults(speechwindow,id,intent_id,function(results) {
connection.end();
res.render('dashboard.ejs', {results: results});
});
}
exports.displayResponse = function(req, res){
generateResponse(req, res);
};
输出窗口:
undefined
https://plot.ly/~me/undefined/
https://plot.ly/~me/undefined.png
me:undefined
undefined
Database is connected ...
As per my forecast, the best strategy has a lighting of 60 percent, zone temperature of 26 degree celsius, and chilled water temperature of 9 degree celsius, and it leads to a power consumption of 1.23 mega watts. You have a curtailment of 268.00 killo watts than the baseline consumption. In order to continue, you may say state the options or you may say exit if you want to quit.
113
7
我希望连接数据库,并在执行getResults函数之前执行查询。在输出窗口中,getResult函数在generateResponse函数之前执行,因为所有函数都是未定义的。我试图将getResult函数作为generateResponse函数的回调,但我得到了相同的输出。 我检查了几个问题,但没有得到明确的答案。我不确定要改变什么,以便该功能以同步的方式运行。我是javascript的新手,也许我对异步执行的理解是有限的,因此我无法解决此错误。
答案 0 :(得分:0)
您需要在getResults
回调中放置connection.query()
来电,而不是之后。