Friends Getting error in below code. please help. Error:
{
"errorType": "TypeError",
"errorMessage": "Cannot read properties of undefined (reading 'slots')",
"trace": [
"TypeError: Cannot read properties of undefined (reading 'slots')",
" at userRegistration (/var/task/index.js:23:38)",
" at Runtime.exports.handler (/var/task/index.js:56:9)",
" at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)"
]
}
Code:
console.log('Loading event');
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var tableName = "GenAITestDemo";
// Close dialog with the customer, reporting fulfillmentState of Failed or Fulfilled
function close(sessionAttributes, fulfillmentState, message) {
return {
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message,
},
};
}
function userRegistration(intent, callback) {
let userInfo = {};
// store every slot we received as part of registration
Object.keys(intent.currentIntent.slots).forEach((item) => {
console.log(item)
userInfo[item] = {"S": intent.currentIntent.slots[item]};
});
// store a registration date
userInfo.registration_date = {"S": new Date().getTime().toString() };
userInfo.UserEmail = {"S": intent.UserEmail};
dynamodb.putItem({
"TableName": tableName,
"Item" : userInfo
}, function(err, data) {
if (err) {
console.log('Failure storing user info');
console.log(err);
callback(close(intent.sessionAttributes, 'Fulfilled',
{'contentType': 'PlainText', 'content': "I am sorry, but something went wrong saving your Registration Info. Please try again."}));
} else {
console.log("Successfully Stored UserInfo");
callback(close(intent.sessionAttributes, 'Fulfilled',
{'contentType': 'PlainText', 'content': "Thank you for registering for our service."}));
}
});
}
// --------------- Main handler -----------------------
// Route the incoming request based on intent.
// The JSON body of the request is provided in the event slot.
exports.handler = (event, context, callback) => {
console.log(event);
try {
userRegistration(event,
(response) => {
callback(null, response);
});
} catch (err) {
callback(err);
}
};
Your issue has nothing to do with DynamoDB, you have an exception where you try to read slots
in an object where it doesn't exist. Focus on this section of code:
Object.keys(intent.currentIntent.slots).forEach((item) => {
console.log(item)
userInfo[item] = {"S": intent.currentIntent.slots[item]};
});
Be sure that intent.currentIntent.slots
exists and that intent.currentIntent.slots[item]
is a valid path also. Print out the values and check them in logs.