-1

aws lambda 함수에서 REST 서비스를 호출하려고하는데 실행 중이므로 콜백 함수 내에 코드가 실행되지 않습니다. REST 서비스는 독립형 node.js 서버에서 올바르게 작동합니다.aws lambda 함수 (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 호출을 위해 사용된다. 하지만 cloudwatch에서 로그를 확인할 때까지는 "새로운 getJson 함수 내부 :"함수 호출 후 로그 메시지가 표시됩니다. 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 
 
}

는 아무도 내가 여기 잘못 뭘하는지 알려 주시기 바랍니다 수 있습니다.

+1

당신은 반환하지 않아야'result' 당신은 콜백에 전달해야한다. 지금은 오류가있을 때만'callback' 함수를 호출합니다. 호출이 성공하면'callback'을 호출해야합니다. NodeJS와 AWS Lambda를 동시에 배우려고하지 않는 것이 좋습니다. Python은 비동기 콜백을 처리 할 필요가 없기 때문에 동시에 배우는 것이 훨씬 쉽습니다. –

+0

안녕하세요, Mark는 결과를 처리하기 위해 콜백 함수를 사용하는 대신 결과를 반환 할 때의 차이점 (부모 함수에서 처리 될 것으로 예상 함)을 이해하게 만드시겠습니까? – Janmajay

+0

함수를 비동기 적으로 호출하면 호출자는 반환 된 값을 기다릴 수 없습니다. 이것이 콜백 함수가 사용되는 이유입니다. 비동기 함수 호출이 작동하는 방식에 대한 근본적인 이해가 빠져 있습니다. 그래서 비동기 호출을 처리 할 필요가 없기 때문에 파이썬에서 함수를 작성하고 값을 반환하는 것이 좋습니다. –

답변

0

결과를 반환하지 마십시오. 당신은 다시 다음과 같은 HTTP 호출의 결과를 얻기 위해 콜백을 사용한다

var req = http.get('url', (res) => { 
 
     var body = ""; 
 

 
     res.on("data", (chunk) => { 
 
      body += chunk 
 
     }); 
 

 
     res.on("end",() => { 
 
      var result = JSON.parse(body); 
 
      
 
      callBack(result) 
 
     }); 
 
    }).on("error", (error) => { 
 
     callBack(err); 
 
});

+0

안녕하세요 Vijayanath, 답장을 보내 주셔서 감사합니다. 결과를 처리하기 위해 콜백 함수를 사용하는 대신 결과를 반환 할 때의 차이점 (상위 함수에서 처리 될 것으로 기대)을 이해하게 만드시겠습니까? – Janmajay