【问题标题】:alexa skill local could not write to dynamodb本地 alexa 技能无法写入 dynamodb
【发布时间】:2020-04-23 17:06:28
【问题描述】:

我正在使用 ask-sdk 编写 node.js 技能,并使用 alexa-skill-local 来测试端点。我需要在其中一个处理程序中将数据持久保存到 DynamoDb。但我不断收到“缺少区域错误”。请在下面找到我的代码:

'use strict';

// use 'ask-sdk' if standard SDK module is installed
const Alexa = require('ask-sdk');

const { launchRequestHandler, HelpIntentHandler, CancelAndStopIntentHandler, SessionEndedRequestHandler } = require('./commonHandlers');

const ErrorHandler = {
    canHandle() {
        return true;
    },
    handle(handlerInput, error) {
        return handlerInput.responseBuilder
            .speak('Sorry, I can\'t understand the command. Please say again.')
            .reprompt('Sorry, I can\'t understand the command. Please say again.')
            .getResponse();
    },
};

////////////////////////////////
// Code for the handlers here //
////////////////////////////////
exports.handler = Alexa.SkillBuilders
    .standard()
    .addRequestHandlers(
        launchRequestHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        ErrorHandler
    )
    .withTableName('devtable')
    .withDynamoDbClient()
    .lambda();

在其中一个处理程序中,我试图获得如下持久属性:

handlerInput.attributesManager.getPersistentAttributes().then((data) => {
    console.log('--- the attributes are ----', data)
})

但我不断收到以下错误:

(node:12528) UnhandledPromiseRejectionWarning: AskSdk.DynamoDbPersistenceAdapter Error: Could not read item (amzn1.ask.account.AHJECJ7DTOPSTT25R36BZKKET4TKTCGZ7HJWEJEBWTX6YYTLG5SJVLZH5QH257NFKHXLIG7KREDKWO4D4N36IT6GUHT3PNJ4QPOUE4FHT2OYNXHO6Z77FUGHH3EVAH3I2KG6OAFLV2HSO3VMDQTKNX4OVWBWUGJ7NP3F6JHRLWKF2F6BTWND7GSF7OVQM25YBH5H723VO123ABC) from table (EucerinSkinCareDev): Missing region in config
    at Object.createAskSdkError (E:\projects\nodejs-alexa-sdk-v2-eucerin-skincare-dev\node_modules\ask-sdk-dynamodb-persistence-adapter\dist\utils\AskSdkUtils.js:22:17)
    at DynamoDbPersistenceAdapter.<anonymous> (E:\projects\nodejs-alexa-sdk-v2-eucerin-skincare-dev\node_modules\ask-sdk-dynamodb-persistence-adapter\dist\attributes\persistence\DynamoDbPersistenceAdapter.js:121:45)

我们可以使用 alexa-skill-local 从 DynamoDb 读取和写入属性吗?我们是否需要一些不同的设置来实现这一点?

谢谢

【问题讨论】:

  • alexa-skill-local 的目的是创建本地开发服务器并在 Alexa 开发控制台中更新端点。您在这里提到的问题与@Mike 在下面的回答中提到的不提供 AWS 配置文件有关,而不是与alexa-skill-local 相关。免责声明:我是alexa-skill-local的创建者。

标签: amazon-dynamodb alexa-skills-kit


【解决方案1】:

我知道这是一个非常古老的话题,但几天前我遇到了同样的问题,我将解释我是如何做到的。 您必须在本地下载 DynamoDB 并按照 here 的说明进行操作

一旦您配置了本地 DynamoDB 并检查它是否正常工作。您必须通过代码将其传递给 DynamoDbPersistenceAdapter 构造函数。 您的代码应类似于:

var awsSdk = require('aws-sdk');
var myDynamoDB = new awsSdk.DynamoDB({
    endpoint: 'http://localhost:8000', // If you change the default url, change it here
    accessKeyId: <your-access-key-id>,
    secretAccessKey: <your-secret-access-key>,
    region: <your-region>,
    apiVersion: 'latest'
});

const {DynamoDbPersistenceAdapter} = require('ask-sdk-dynamodb-persistence-adapter');
return new DynamoDbPersistenceAdapter({
    tableName: tableName || 'my-table-name',
    createTable: true,
    dynamoDBClient: myDynamoDB
});

&lt;your-acces-key-id&gt;&lt;your-secrete-access-key&gt;&lt;your-region&gt; 在 aws 配置和凭据文件中定义。

下一步是像往常一样使用alexa-skill-local 命令启动您的服务器。

希望这会有所帮助! =)

【讨论】:

    【解决方案2】:

    大概您有一个 AWS 配置文件,您的技能在本地运行时正在使用该配置文件。

    您需要编辑 .config 文件并在那里设置默认区域(即 us-east-1)。该区域应与您的表所​​在的区域相匹配。

    或者,如果您希望能够完全隔离运行,您可能需要编写条件逻辑并将 dynamo 客户端与针对您计算机上运行的 DynamoDB Local 实例的客户端交换。

    【讨论】:

      猜你喜欢
      • 2020-09-19
      • 2019-07-28
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 2017-10-22
      相关资源
      最近更新 更多