LoginSignup
0
0

More than 3 years have passed since last update.

AlexaのS3 adaperでgetPersistentAttributesがエラーを投げる時

Last updated at Posted at 2019-07-07

s3 adapterはレコード(S3オブジェクト)が無いとエラーをthrowします。
なので初回起動時だけエラーになることがあります。

こういうヘルパー関数入れておくと、回避できます。

Typescript

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const getPersistentAttributes = async <Attributes = {[key: string]: any}>(handlerInput: HandlerInput, defaultAttributes: Attributes): Promise<Attributes> => {
    try {
        const data = await handlerInput.attributesManager.getPersistentAttributes()
        if (!data) return defaultAttributes
        return data as Attributes
    } catch (e) {
        return defaultAttributes
    }
}

Node.js

const getPersistentAttributes = async (handlerInput, defaultAttributes = {}) => {
    try {
        const data = await handlerInput.attributesManager.getPersistentAttributes()
        if (!data) return defaultAttributes
        return data
    } catch (e) {
        return defaultAttributes
    }
}

めんどくさいという人へ

ライブラリにこのコードいれてます。
https://github.com/ask-utils/ask-utils/blob/master/libs/attributeManager.ts#L21

$ npm i -S ask-utils

Hosted skillならpackage.jsonにこうかけばOK

“ask-utils”: “2”
const { getPersistentAttributes } = require(‘ask-utils’)

const att = await getPersistentAttributes(handlerInput, {count: 0})
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0