[英]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.