使用Lambda以Alexa技能将数据写入dynamoDB

时间:2019-03-06 19:25:29

标签: node.js aws-lambda amazon-dynamodb alexa

我有一个与Alexa技能有关的lambda函数。在开始这项技能时,我想将一个字符串存储到dynamoDB。因此,我直接在启动请求中调用一个函数(该函数称为nameExport())。 但是,仅当我在启动请求中删除“ this.emit”命令时,该函数才会存储某些内容。如果我离开“ this.emit”,该功能将起作用,但不会将任何内容存储到数据库中。在下面,您可以找到有关LaunchRequest的代码以及存储到dynamoDB中的函数。

var config = require("config");
var Alexa = require("alexa-sdk");
var AWS = require("aws-sdk");
var Speech = require("ssml-builder");
var dynamodb = new AWS.DynamoDB();

let handlers = {
    'LaunchRequest': function () {
        nameExport("testName");
        let speech = new Speech ();
        speech.say("Welcome by Emotions.")
        speech.pause("100ms")
        speech.say("Do you want to assess your current emotion?")
        let speechOutput = speech.ssml(true);
        this.emit(":ask", speechOutput)
    },
    'Emotion': function () {
        let speech = new Speech();
        ...

应该将String写入数据库的nameExport函数:

function nameExport(name) {
    var tableName = "myinfo";
    dynamodb.putItem({
            "TableName": tableName,
            "Item": {
                "name": {
                    "S": name
                }
            }
        }, function(err, data) {
            if (err) {
                //context.fail('ERROR: Dynamo failed: ' + err);
            } else {
                console.log('Dynamo Success: ' + JSON.stringify(data, null, '  '));
                //context.succeed('SUCCESS');
            }
        });
};   

1 个答案:

答案 0 :(得分:0)

this.emit仅完成lambda执行,而putItem函数无法在其作用域内完成。 this.emit完成后,您应该致电putItem

您可以通过使用async/await(确保lambda使用node.js v8.10)来实现:

'LaunchRequest': async function () {
        await nameExport("testName");
        let speech = new Speech ();
        speech.say("Welcome by Emotions.")
        speech.pause("100ms")
        speech.say("Do you want to assess your current emotion?")
        let speechOutput = speech.ssml(true);
        this.emit(":ask", speechOutput)
    },

async function nameExport(name) {
    var tableName = "myinfo";
    return new Promise((resolve, reject) => { dynamodb.putItem({
            "TableName": tableName,
            "Item": {
                "name": {
                    "S": name
                }
            }
        }, function(err, data) {
            if (err) {
                reject(err);
            } else {
                console.log('Dynamo Success: ' + JSON.stringify(data, null, '  '));
                resolve();
            }
        })});
};  

或者没有async/await的情况下,通过在回调中发出“:ask”来解决问题,

'LaunchRequest': function () {

        let speech = new Speech ();
        speech.say("Welcome by Emotions.")
        speech.pause("100ms")
        speech.say("Do you want to assess your current emotion?")
        let speechOutput = speech.ssml(true);
        nameExport("testName", () => this.emit(":ask", speechOutput));
    },

function nameExport(name, callback) {
    var tableName = "myinfo";
    return new Promise((resolve, reject) => { dynamodb.putItem({
            "TableName": tableName,
            "Item": {
                "name": {
                    "S": name
                }
            }
        }, function(err, data) {
            if (err) {
                // some error handling
            } else {
                callback()
            }
        })});
};  

顺便说一句-不再支持您要使用的Alexa SDK 2.0-1.0。我建议切换到最新版本(通过重新编写代码或使用adapter