Alexa SDK:无法获取persitentAttributes

时间:2019-06-03 11:11:35

标签: alexa-skills-kit

我正在尝试向我的lambda函数添加持久属性。

我创建了一个dynamoDB表并将其添加到我的lambda函数的触发器中。 我从github复制了一个示例代码,但是当我尝试启动该技能时,我得到了一个错误。控制台日志显示:

{
    "errorMessage": "Could not read item (amzn1.ask.account.AGIIYNRXWDLBD6XEPW72QS2BHGXNP7NWYBEWSH2XLSXZP64X3NCYEMVK233VFDWH77ZB6DAK6YJ53SZLNUFVQ56CYOVCILS7QFZI4CIRDWC3PAHS4QG27YUY5PTT6QEIK46YFNTJT54YAKNGOWV2UO66XZACFDQ5SEXKJYOBNFNIZNUXKNTIAAYZG4R5ZU4FMLPDZZN64KLINNA) from table (Spiele): The provided key element does not match the schema",
    "errorType": "AskSdk.DynamoDbPersistenceAdapter Error",
    "stackTrace": [
        "Object.createAskSdkError (/var/task/node_modules/ask-sdk-dynamodb-persistence-adapter/lib/utils/AskSdkUtils.js:22:17)",
        "DynamoDbPersistenceAdapter.<anonymous> (/var/task/node_modules/ask-sdk-dynamodb-persistence-adapter/lib/attributes/persistence/DynamoDbPersistenceAdapter.js:123:49)",
        "step (/var/task/node_modules/ask-sdk-dynamodb-persistence-adapter/lib/attributes/persistence/DynamoDbPersistenceAdapter.js:44:23)",
        "Object.throw (/var/task/node_modules/ask-sdk-dynamodb-persistence-adapter/lib/attributes/persistence/DynamoDbPersistenceAdapter.js:25:53)",
        "rejected (/var/task/node_modules/ask-sdk-dynamodb-persistence-adapter/lib/attributes/persistence/DynamoDbPersistenceAdapter.js:17:65)",
        "<anonymous>",
        "process._tickDomainCallback (internal/process/next_tick.js:228:7)"
    ]
}

该表包含主键“名称”和排序键“ UserId”。那是错的吗?

这是我的index.js:

const Alexa = require('ask-sdk');

// Define the skill features
let skill;

/**
 * If this is the first start of the skill, grab the user's data from Dynamo and 
 * set the session attributes to the persistent data. 
 */
const GetUserDataInterceptor = {
    process(handlerInput) {
        let attributes = handlerInput.attributesManager.getSessionAttributes();
        if (handlerInput.requestEnvelope.request.type === 'LaunchRequest' && !attributes['isInitialized']) {
            return new Promise((resolve, reject) => {
                handlerInput.attributesManager.getPersistentAttributes()
                    .then((attributes) => {
                        attributes['isInitialized'] = true;
                        saveUser(handlerInput, attributes, 'session');
                        resolve();
                    })
                    .catch((error) => {
                        reject(error);
                    })
            });
        }
    }
};

function saveUser(handlerInput, attributes, mode) {
        if(mode === 'session'){
            handlerInput.attributesManager.setSessionAttributes(attributes);
        } else if(mode === 'persistent') {
            console.info("Saving to Dynamo: ",attributes);
            return new Promise((resolve, reject) => {
                handlerInput.attributesManager.getPersistentAttributes()
                    .then((persistent) => {
                        delete attributes['isInitialized'];
                        handlerInput.attributesManager.setPersistentAttributes(attributes);

                        resolve(handlerInput.attributesManager.savePersistentAttributes());
                    })
                    .catch((error) => {
                            reject(error);
                    });
            });
        }
   }

 const LaunchHandler = {
        canHandle(handlerInput) {
            return handlerInput.requestEnvelope.request.type === 'LaunchRequest';

        },
        handle(handlerInput) {
            console.info("LaunchRequest");
            let attributes = handlerInput.attributesManager.getSessionAttributes();
            console.info("Test the load: " + attributes['isInitialized']);

            attributes['FOO'] = "BAR";
            saveUser(handlerInput, attributes, 'persistent');

            return handlerInput.responseBuilder
                .speak('Hello')
                .reprompt('Hello')
                .getResponse();
        }
    }

exports.handler = Alexa.SkillBuilders.standard()
    .addRequestHandlers(
        LaunchHandler
    )
    .addRequestInterceptors(GetUserDataInterceptor)
    .withTableName('Spiele')
    .withAutoCreateTable(true)
    .withDynamoDbClient()
    .lambda();

有人可以告诉我我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

请确认分区键是“ userId”而不是“ UserId”(注意大写的U)。

我也建议使用'this'对象。

让我知道是否有帮助。

欢呼