question

5da9cfb1-01c6-48f0-b917-e07d16683e4b avatar image
5da9cfb1-01c6-48f0-b917-e07d16683e4b asked

How can I save a received slot value from a skill into DynamoDB?

I have an AWS skill with a Lambda function that's based on the Interactive Adventure Game Skill template in Node.js. I decided to add a an extra AMAZON.NUMBER slot in one area, and everything is working in terms of the intent and Alexa recognizing the number.

However, i'm not sure how to save that value into the database.

The /handlers/dynamoDB.js file has putUserState and putSetState already working, but those functions only have session and cb passed to them as parameters. The value I need to save is in the request.intent.slots.PhoneNumber.value field, but it's obviously undefined at that point.

Therefore, I tried creating and exporting a new helper function that would be called for only that particular intent. It's in the same Dynamo helper file.

function putRequestData ( session, phoneNo, cb ) {
  var phoneNo = phoneNo
  var params = {
    "TableName": config.dynamoTableName,
    "Item": {
      "optionId": session.user.userId,
      "breadcrumbs": session.attributes.breadcrumbs,
      "currentSceneId": session.attributes.currentSceneId,
      "phoneNumber": phoneNo
    }
  }
  docClient.put( params, handler( cb ) )
}

It's called in /handlers/intentHandlers_generated.js, where intent is already being passed in the primary function (dynamo is already exported and added to this file).

"PhoneNumberIntent": function ( intent, session, request, response, cb ) {
	processUtterance( intent, session, request, response, "my phone number is {PhoneNumber}" )
	var phoneNo = intent.slots.PhoneNumber.value
	console.log(phoneNo)
	dynamo.putRequestData( phoneNo, cb )
}

but this gives me this error in cloudwatch logs for the Lambda function:


{
            "ingestionTime": 1503528160904,
            "timestamp": 1503528145733,
            "message": "2017-08-23T22:42:25.715Z\t55d56bf4-8854-11e7-a18b-6b9f4eb70ec4\tUnexpected exception TypeError: Cannot read property 'userId' of undefined\n",
            "eventId": "33529798076810850770027871722698209522952682213984370690",
            "logStreamName": "2017/08/23/[$LATEST]ec0f2f318c454bb9a0eea3bc1dc943c1"
        },
        {
            "ingestionTime": 1503528160904,
            "timestamp": 1503528145773,
            "message": "2017-08-23T22:42:25.736Z\t55d56bf4-8854-11e7-a18b-6b9f4eb70ec4\t{\"errorMessage\":\"Cannot read property 'userId' of undefined\",\"errorType\":\"TypeError\",\"stackTrace\":[\"Object.putRequestData (/var/task/handlers/dynamoDB.js:67:31)\",\"module.exports.PhoneNumberIntent (/var/task/handlers/intentHandlers_generated.js:57:10)\",\"eventHandlers.onIntent (/var/task/handlers/eventHandlers.js:52:21)\",\"AlexaSkill.requestHandlers.IntentRequest (/var/task/AlexaSkill.js:28:33)\",\"AlexaSkill.execute (/var/task/AlexaSkill.js:79:20)\"]}\n",
            "eventId": "33529798077702880577969096648359638253858616674223587331",
            "logStreamName": "2017/08/23/[$LATEST]ec0f2f318c454bb9a0eea3bc1dc943c1"
        }

Any ideas? Thank you!
alexa skills kitskillintentsnode_js
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Brian@Amazon avatar image
Brian@Amazon answered

Can you try logging the full request your service receives to see why userId may not be present? Is a different request than what you are expecting being sent?

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.