Alexa自定义技能DynamoDB.Node.js ResponseBuilder不等待异步调用完成

编程入门 行业动态 更新时间:2024-10-09 19:12:55
本文介绍了Alexa自定义技能DynamoDB.Node.js ResponseBuilder不等待异步调用完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不熟悉Node.js和Javascript,并且正在使用Lambda函数和DynamoDB开发Alexa应用程序。 我在DynamoDB中有一个表,名为:使用PrimaryKey: Said和 say列聊天。每当启动Alexa技能时,我只想获取基于用户所说内容的记录并返回。因此,基本上在主键上执行一次查询就可以了。 但是,语音输出变量中的lambda函数没有得到任何响应,因为API不等待响应生成器完成对DynamoDB的异步调用并返回空响应。 在发送响应之前,是否有任何方法可以强制解决异步调用?

I am new to Node.js and Javascript and am developing an Alexa application using Lambda function and DynamoDB. I have a table in DynamoDB named: Chat with PrimaryKey: 'Said' and a column 'say'. Whenever the Alexa skills is launched I just want to fetch a record based on what is said by the user and return. So its basically a single Query on the primary key which works fine. However, I dont get any response from the lambda function in speech output variable as the API doesn't wait for the response builder to complete the async call to DynamoDB and returns a null response. Is there any way to enforce the async call to be resolved before sending the response?

const WelcomeMessage = { canHandle(handlerInput) { const request = handlerInput.requestEnvelope.request; return request.type === 'LaunchRequest' || (request.type === 'IntentRequest'); }, handle(handlerInput) { var ans; var AWS = require('aws-sdk'); // Set the region AWS.config.update({ region: 'us-east-1' }); // Create the DynamoDB service object var dynamodb = new AWS.DynamoDB(); var params = { TableName: 'chat', Key: { 'said': { S: 'Hi Sir' + '' } }, ProjectionExpression: 'say' }; dynamodb.getItem(params, function(err, data) { if (err) { console.log(err, err.stack); } else { if (data) { return handlerInput.responseBuilder .speak(data.Item.say.S + '') .getResponse(); } else { ans = 'You dint train me for that!'; return handlerInput.responseBuilder .speak(ans) .getResponse(); } } }); } };

错误的输出:

推荐答案

我找到了一种解决方法。我返回一个诺言并在将其返回之前将其解析,以确保在发送响应之前完成回调。

I found a workaround. I return a promise and resolve it before I return it ensuring the callback to be completed before a response is sent.

const WelcomeMessage = { canHandle(handlerInput) { const request = handlerInput.requestEnvelope.request; return request.type === 'LaunchRequest' || (request.type === 'IntentRequest'); }, handle(handlerInput) { return new Promise((resolve) => { var ans; var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'us-east-1'}); // Create the DynamoDB service object //ddb = new AWS.DynamoDB({apiVersion: '2012-10-08'}); var dynamodb = new AWS.DynamoDB(); var params = { TableName : 'chat', Key: { 'said':{S: handlerInput.requestEnvelope.request.intent.slots.input.value+''} } }; dynamodb.getItem(params, function(err, data) { if (err){ console.log(err, err.stack); } else{ if(data.Item){ return resolve(handlerInput.responseBuilder .speak(data.Item.say.S+'') .withShouldEndSession(false) .getResponse()); } else{ ans='You dint train me for that!'; return resolve(handlerInput.responseBuilder .speak(ans) .withShouldEndSession(false) .getResponse()); } } }); }); } };

更多推荐

Alexa自定义技能DynamoDB.Node.js ResponseBuilder不等待异步调用完成

本文发布于:2023-11-28 03:00:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640707.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   技能   Alexa   DynamoDB   ResponseBuilder

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!