aws lambda関数からRESTサービスを呼び出そうとしていますが、コールバック関数内でコードが実行されていません。 RESTサービスは、スタンドアロンのnode.jsサーバー上で正常に動作しています。awsラムダ関数(Alexaスキル用)でhttpsモジュールが動作しません。
CourseInfoIntentでは、私はスロット値を取得してエンドポイントURLを作成しています。私は必要な値でhttps_options_getを作成しました。
exports.handler = function(event, context, callback){
console.log("Course Info Skill Started");
var alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
const handlers = {
'LaunchRequest': function(){
console.log("Inside Launch Request");
this.emit('CourseInfoIntent');
},
'CourseInfoIntent': function(){
// fetch the lesson id from data as per slot_type(Lesson) value
console.log("Course Info Intent Started");
//this.event.request.intent.slots.slotname.value for retrieving slot value from utterance
var req_lesson = this.event.request.intent.slots.Lesson.value;
console.log("Lesson Requested: " + req_lesson);
var lessonId = data[req_lesson];
console.log("Lesson ID: " + lessonId);
path = path + '?LessonID='+lessonId;
var url = "https://" + hostName + path;
console.log("Rest url is :-> " + url);
// create opyions for https request
var https_options_get = {
host: hostName,
method: 'GET',
path: path,
headers: {
'Content-Type': 'application/json'
}
};
var result = getJson(https_options_get, function(err){
console.log("Inside callback function");
const speechOutput = "There has been an error with your request" + err;
this.response.speak(speechOutput);
this.emit(':responseReady');
});
const speechOutput = "The name of the lesson is " + result;
const reprompt = "Do you like to know about more sessions? Answer Yes or No";
this.response.speak(speechOutput).listen(reprompt);
this.emit(':responseReady');
}
getJson機能は、Node.jsのhttpsのモジュールを使用してREST呼び出しを行うために使用されます。しかし、クラウドウォッチのログをチェックすると、関数呼び出しの後にログメッセージが表示されるだけです。var request = https.request(https_options_get、function(response){}はトリガーしません)
function getJson(https_options_get, context, callback) {
console.log("Inside new getJson function: ");
var request = https.request(https_options_get, function (response) {
console.log('In GET Request Function block');
var body = '';
response.on('data', function (chunk) {
body += chunk;
});
response.on('end', function() {
var bodyJSON = JSON.parse(body);
console.log('bodyJSON:-> ' + bodyJSON);
var result = bodyJSON.LessonName;
console.log('result:-> ' + result);
return result;
});
response.on('error', callback);
})
.on('error', callback)
.end();
// end() should be placed above so that the control know we are done with the request and it can now send it to server
}
誰もが私がここで間違ってやって教えてくださいことができます。
あなたは、コールバックに渡すべきであるresult' '返すべきではありません。現在は、エラーがあった場合にのみ 'callback'関数を呼び出します。呼び出しが成功すると、 'callback'を呼び出す必要があります。 NodeJSとAWS Lambdaを同時に学習しようとは勧めません。 Pythonは、非同期コールバックに対処する必要がないため、同時に学習する方がはるかに簡単です。 –
こんにちは、コールバック関数を使用して結果を処理するのではなく、結果を返す際の違い(親関数で処理されることを期待している)を教えてください。 – Janmajay
関数を非同期に呼び出すと、呼び出し元は戻り値を待つことができません。これがコールバック関数が使用される理由です。非同期関数呼び出しがどのように機能するかを基本的に理解していません。これは、私があなたの関数をPythonで書くことを提案した理由です。なぜなら、非同期呼び出しを扱う必要がなく、戻り値が期待通りに機能するからです。 –