前提・実現したいこと
Alexaを購入したので、アプリを自宅用に作成しています。
node.jsでalexaの情報を受けて、dynamoを更新してから返答を返したいです。
かなり長いですが、よろしくお願いします。
該当のソースコード
JavaScript
1// define and require 2const Alexa = require("alexa-sdk"); 3const APP_ID = undefined; 4var AWS = require('aws-sdk'); 5var dynamoDoc = new AWS.DynamoDB.DocumentClient({region: 'ap-northeast-1'}); 6const languageStrings = { 7 "ja": { "translation": {"STOP_MESSAGE": "スキルを終了します。",...}}}; 8dataEmitter = new myEmitter(); 9 10// dynamo process 11function dynamoLogHandler(data) { 12 var params_update = { 13 TableName:'test_table', 14 Key:{"eventId":data.request.intent.solts.test.name}, 15 AttributeUpdates:{ 16 "points":{'Action':'ADD','Value': (- this.request.intent.solts.test.value)}}, 17 ReturnValues:"UPDATED_NEW" 18 }; 19 dynamoDoc.update(params_update,function(err,data_updated){ 20 let speechOutput = String(data_updated.Attributes.points)+"更新しました"; 21 }); 22 // 今回修正した処理 23 dataEmitter.emit('data',speechOutput) 24 // 前回質問していた時の処理-> thisがundefinedになる 25 //this.response.speak(speechOutput); 26 //this.emit(':responseReady'); 27} 28 29// test intent handler 30function handlerTest(userGaveUp){ 31 dynamoLogHandler(this); 32 // 現在の処理 -> ここまでで有れば正常にdynamoUpdateが走る 33 let speechOutput = dataEmitter.on('data',function(speechOutput){return speechOutput;})); 34 // トライした処理 -> この先を入れるとresponseが[Object Object] 35 // this.response.speak(speechOutput); 36 // this.emit(':responseReady'); 37} 38 39// intent integrated handler 40const handlers = { 41 "LaunchRequest": function () {...}, 42 'Unhandled': function () {...}, 43 "testIntent": function() { 44 handlerTest.call(this, false); 45 } 46} 47 48exports.handler = function (event, context) { 49 const alexa = Alexa.handler(event, context); 50 alexa.APP_ID = APP_ID; 51 alexa.resources = languageStrings; 52 alexa.registerHandlers(handlers); 53 alexa.execute(); 54};
全体の流れ
- alexa handlerでtestIntentが設定されます。
- testIntentHandlerが実行され、dynamoLogHandlerが呼び出されます。
- dynamoDBを更新します(ここにはミスはないです)
- dynamoLogHandlerの処理が終了すると、emitterでtestIntentHandlerに戻ります
(let speechOutput = dataEmitter.on('data',function);
5. this.emit(':tell',speechOutput)
で発話内容をAlexaに返します
問題点
4番までで実行すると、全てが正常に実行されて進むのですが、
この、5番を入れると、emitterが[Object Object]として定義されてしまい、3,4を実行せずに終了してしまいます。
試したこと&分かっていないこと
emitter
を利用することで、
this.emit
の位置を変えたり、return
を使ったり、emitter
を使ったりしましたが、
this
がscopeを超えて引き継がれなかったり、そもそもreturnの位置が間違えていたりと、
全くうまくいっていません。
また、sdkを参照して作っているのですが、userGaveUp
が何を指すのかどのような使い方ができるのかが不明です。
全ての処理は記載していないですが、うまくいっていない部分は上記になります。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。