2017-04-11 69 views
0

我想創建一個Alexa技術,查找基於的Alexa設備的位置的Web服務上的位置。Alexa的NodeJS沒有語音輸出沒有一個具體的錯誤

我有所有的代碼工作,因爲我可以在日誌中看到正確的輸出,但我努力的一件事就是讓語音輸出工作。

任何幫助將不勝感激。我對NodeJS很陌生。我有一種感覺,這與在https.request中調用tell相關,但現在我不知道如何解決這個問題。

代碼:

deviceAddressRequest.then((addressResponse) => { 
    switch(addressResponse.statusCode) { 
     case 200: 
      console.log("Address successfully retrieved, now responding to user."); 
      const address = addressResponse.address; 
      const postcode = `${address['postalCode']}`; 

      // Get JSON response 

      console.info("test begin"); 

      var post_data = 
      { 
       "postcode": 
       "`${address['postalCode']}`" 
      }; 
      console.info ('Users postcode is ' + postcode); 
      var post_options = { 
       host: '[redacted]', 
       port: '443', 
       path: '/alexa/address.php?postcode=' + encodeURI(postcode), 
       method: 'POST', 
       headers: { 
        'Content-Type': 'application/json', 
        'Content-Length': Buffer.byteLength(JSON.stringify(post_data)) 
       } 
      }; 
      console.info ('Post options set'); 
      var post_req = https.request(post_options, function(res) { 
       res.setEncoding('utf8'); 
       var returnData = ""; 
       res.on('data', function (chunk) { 
        returnData += chunk; 
       }); 
       res.on('end', function() { 
        console.info("Data returned!") 
        console.info('returnData: ' + returnData); 
        var nearBoutique = JSON.parse(returnData).location1; 
        console.info("Nearest location: " + nearLocation); 
        const ADDRESS_MESSAGE = "Your nearest location is: " + nearLocation; 
       }); 
      }); 
      post_req.write(JSON.stringify(post_data)); 
      post_req.end(); 
      console.info(ADDRESS_MESSAGE); 
      // I tried it here, and also under the "const ADDRESS_MESSAGE" 
      this.emit(":tell", ADDRESS_MESSAGE); 
      break; 
     case 204: 
      console.log("Successfully requested from the device address API, but no address was returned."); 
      this.emit(":tell", Messages.NO_ADDRESS); 
      break; 
     case 403: 
      console.log("The consent token we had wasn't authorized to access the user's address."); 
      this.emit(":tellWithPermissionCard", Messages.NOTIFY_MISSING_PERMISSIONS, PERMISSIONS); 
      break; 
     default: 
      this.emit(":ask", Messages.LOCATION_FAILURE, Messages.LOCATION_FAILURE); 
    } 

    console.info("Ending getAddressHandler()"); 
}); 

而且LAMBDA CloudWatch的日誌:

START RequestId: *** Version: $LATEST 
Beginning execution for skill with APP_ID=amzn1.ask.skill.*** 
Starting newSessionRequestHandler() 
Starting getAddressHandler() 
Creating AlexaAddressClient instance. 
Ending newSessionRequestHandler() 
Ending execution for skill with APP_ID=amzn1.ask.skill.*** 
Device Address API responded with a status code of : 200 
Address successfully retrieved, now responding to user. 
test begin 
Users postcode is *** *** 
Post options set 
Data returned! 
returnData: {"location1":"123 Street Name, London, is currently open.","location2":"123 Street Name, London, is currently open.","store3":"123 Street Name, London, is currently open.","postcode":*** ***} 
Nearest location: 123 Street Name, London, is currently open. 
END RequestId: *** 
REPORT RequestId: Duration: 1869.51 ms Billed Duration: 1900 ms Memory Size: 128 MB Max Memory Used: 31 MB 
START RequestId: Version: $LATEST 
Beginning execution for skill with APP_ID=amzn1.ask.skill.*** 
Starting sessionEndedRequestHandler() 
Ending sessionEndedRequestHandler() 
Ending execution for skill with APP_ID=amzn1.ask.skill.*** 
END RequestId: 
REPORT RequestId: Duration: 15.49 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 31 MB 

我不是在尋找一個直接的解決方案,但在正確的方向指針將是巨大的,請。

感謝

回答

1

它出現的是與如何this關鍵字在JavaScript的工作,你會想看看this link from mdn瞭解更多一點關於它是如何工作的,以及它是如何工作方面的問題到箭頭功能。

+0

一個非完美的解決方案是在原始Alexa調用的範圍內定義一個變量,然後再引用它。 「讓thiz =這個; ... thiz.emit ...」 – wblaschko