繁体   English   中英

Dynamodb Alexa查询未调用数据项

[英]Dynamodb alexa query not calling data item

我正在创建一种技能,我想打电话给Alexa在特定的日期和时间阅读不同的项目。 目前,我的表设置如下:Date | Time | State

日期设置为我的主键,时间设置为我的排序键。 我将设置日期和时间设置为ASK中的广告位值,并且可以看到这些值正在传递。 我还确保在dynamodb中我的日期和时间格式正确。

我的问题是,当我致电alexa并要求某个日期和时间的状态时,我无法让alexa响应与该日期和时间相对应的状态。

谁能帮我这个? 或告诉我我要去哪里错了,我将在下面插入代码。

 const awsSDK = require('aws-sdk'); const updatedincident = 'updatedincident'; const docClient = new awsSDK.DynamoDB.DocumentClient(); var AWSregion = 'us-east-1'; // us-east-1 var AWS = require('aws-sdk'); var dbClient = new AWS.DynamoDB.DocumentClient(); AWS.config.update({ region: "'us-east-1'" }); let GetMachineStateIntent = (context, callback, dateSlot, timeSlot) => { var params = { TableName: "updatedincident", KeyConditionExpression: 'date = :dVal and time < :tVal', ExpressionAttributeValues: { ':dVal': dateSlot, ':tVal': timeSlot }, ScanIndexForward: false // gets values in reverse order by time }; dbClient.query(params, function (err, data) { if (err) { // failed to read from table for some reason.. console.log('failed to load data item:\\n' + JSON.stringify(err, null, 2)); // let skill tell the user that it couldn't find the data sendResponse(context, callback, { output: "the data could not be loaded from your database", endSession: false }); } else { let dataItem = data.Items[0]; console.log('loaded data item:\\n' + JSON.stringify(dataItem, null, 2)); // assuming the item has an attribute called "state".. sendResponse(context, callback, { output: dataItem.state, endSession: false }); } }); }; function sendResponse(context, callback, responseOptions) { if(typeof callback === 'undefined') { context.succeed(buildResponse(responseOptions)); } else { callback(null, buildResponse(responseOptions)); } } function buildResponse(options) { var alexaResponse = { version: "1.0", response: { outputSpeech: { "type": "SSML", "ssml": `<speak><prosody rate="slow">${options.output}</prosody></speak>` }, shouldEndSession: options.endSession } }; if (options.repromptText) { alexaResponse.response.reprompt = { outputSpeech: { "type": "SSML", "ssml": `<speak><prosody rate="slow">${options.reprompt}</prosody></speak>` } }; } return alexaResponse; } exports.handler = (event, context, callback) => { try { var request = event.request; if (request.type === "LaunchRequest") { sendResponse(context, callback, { output: "welcome to my skill, I can tell you about the status of machines at different times. what data are you looking for?", endSession: false }); } else if (request.type === "IntentRequest") { if (request.intent.name === "GetMachineStateIntent") { var dateSlot = request.intent.slots.Date != null ? request.intent.slots.Date. value : null; var timeSlot = request.intent.slots.Time != null ? request.intent.slots.Time.value : null; // pass the slot values to the GetMachineStateIntent function GetMachineStateIntent(context, callback, dateSlot, timeSlot); } else if (request.intent.name === "AMAZON.StopIntent" || request.intent.name === "AMAZON.CancelIntent") { sendResponse(context, callback, { output: "ok. good bye!", endSession: true }); } else if (request.intent.name === "AMAZON.HelpIntent") { sendResponse(context, callback, { output: "you can ask me about incidents that have happened or states of machines in the past", reprompt: "what can I help you with?", endSession: false }); } else { sendResponse(context, callback, { output: "I don't know that one! please try again!", endSession: false }); } } else if (request.type === "SessionEndedRequest") { sendResponse(context, callback, ""); // no response needed } else { // an unexpected request type received.. just say I don't know.. sendResponse(context, callback, { output: "I don't know that one! please try again!", endSession: false }); } } catch (e) { // handle the error by logging it and sending back an failure console.log('Unexpected error occurred in the skill handler!', e); if(typeof callback === 'undefined') { context.fail("Unexpected error"); } else { callback("Unexpected error"); } } }; 

我上面的代码当前得到的响应是

'the data could not be loaded from your database'

云手表也告诉我这个

 2018-05-16T09:29:06.635Z 93d4b6e6-58eb-11e8-b686-597d65771e90 failed to load data item: { "message": "Invalid KeyConditionExpression: Attribute name is a reserved keyword; reserved keyword: date", "code": "ValidationException", "time": "2018-05-16T09:29:06.633Z", "requestId": "EQPQTAGO4QKH9SM5GSOA9O3DDFVV4KQNSO5AEMVJF66Q9ASUAAJG", "statusCode": 400, "retryable": false, "retryDelay": 35.56027710686527 } 

GetMachineStateIntent函数中,尝试更改如下的params结构:

var params = {
  TableName: "updatedincident",
  KeyConditionExpression: '#d = :dVal and #t < :tVal',
  ExpressionAttributeValues: {
     ':dVal': dateSlot,
     ':tVal': timeSlot
  },
  ExpressionAttributeNames: {
     '#d': 'date',
     '#t': 'time'
  },
  ScanIndexForward: false // gets values in reverse order by time 
};

看来date一词是一个保留关键字,因此它不能直接在date = :dVal类的表达式中使用,这就是为什么必须为其赋予属性名别名( #d )并映射回实际属性名的原因( date )。

在DynamoDB中,您必须使用两个键,即主键和主排序键。 该查询基于这两个键搜索请求的值。

试试我的代码:

'FindName': function() {

    var tableName = "CVRaman";
    var userName = "Prateek";
    var userId = "kapoor";

    const dynamodbParams = {
        TableName: tableName,
        Key: {
            userId: userId,
            userName: userName,
        },
        ProjectionExpression: 'userName', //Projection Expression is used to select only specific columns which we want to get
    };

    console.log("Attempting to find name", dynamodbParams);
    dynamoDb.get(dynamodbParams).promise()
        .then(data => {
            console.log('name found', dynamodbParams);
            console.log(data.Item.userName);
            var a = data.Item.userName;
            console.log(a);
            this.emit(':ask', 'Name as ' + a);

        })
        .catch(err => {
            console.log(err);
            this.emit(':tell', 'we have a problem');
        });
},

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM