0
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);
};
輸出窗口:其中功能被執行的順序是異步(JavaScript)的
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新手,也許我對asynchoronous執行的理解是有限的,因此我無法解決這個錯誤。
這是什麼意思*函數執行的順序是異步*?該訂單既不同步也不異步。 – skypjack
@skypjack我相信問題是爲什麼回調沒有按預期順序執行。 – doublesharp
您能否將您的問題標題編輯爲問題標題?這也很難理解你所面臨的問題。 – k0pernikus