I am trying to write some data into DynamoDb. I’ve created the table with a partition key called s3jsonfile (String), and a sort key called messageId (String).
My main goal is to use this table to track the progress of my processing. Since my logic is based on receiving SQS messages, i’m gonna write there how many entries from the SQS message were processed, total number of entries, message number and the s3 key.
For writing, i’m passing to the function the s3jsonfile (partitionkey) and messageId(sort key).
I’ve verified the data i’m passing multiple times, its even included on my debugging. However, i’m always receiving this error:
Error initializing message processing: TypeError: Cannot read properties of undefined (reading '0') at Object.visit (/home/ec2-user/node-project/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:1003:36) at se_AttributeValue (/home/ec2-user/node-project/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:2544:25) at /home/ec2-user/node-project/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:2743:16 at Array.reduce (<anonymous>) at se_ExpressionAttributeValueMap (/home/ec2-user/node-project/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:2739:32) at ExpressionAttributeValues (/home/ec2-user/node-project/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:3102:39) at applyInstruction (/home/ec2-user/node-project/node_modules/@smithy/smithy-client/dist-cjs/index.js:1092:27) at take (/home/ec2-user/node-project/node_modules/@smithy/smithy-client/dist-cjs/index.js:1060:5) at se_UpdateItemInput (/home/ec2-user/node-project/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:3096:40) at se_UpdateItemCommand (/home/ec2-user/node-project/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:1360:25) Parameters used: { TableName: 'messageProcessingTable', Key: { s3jsonfile: 'converted_data_1726002816321.json', messageId: 'Message 1 of 1' }, UpdateExpression: 'SET totalEntries = :totalEntries, entriesProcessed = :entriesProcessed, processingStatus =
:processingStatus, lastUpdated = :timestamp’,
ExpressionAttributeValues: {
‘:totalEntries’: ‘6’,
‘:entriesProcessed’: ‘0’,
‘:processingStatus’: ‘0’,
‘:timestamp’: ‘2024-09-10T21:13:45.990Z’
}
}
I’ve double checked my schema, my function to write the data, and i still can’t figure out the problem. Also, i’m using AWS SDK v3 so there is no need to pass S for string and N for number (thats why i’m passing to the update command the entire string).
The error is happening because i still have no data on this table?
The goal on this specific method is to initialize writing the data on this s3jsonfile (key) with the values i provided inside the method (sorting key).
I’m kinda new to DynamoDB. I don’t know if i should create my table in another way (without a sorting key maybe). Any input is highly appreciated, and sorry for the english.
Below is my code. Any additional information i’ll be glad to reply. If something on my post isn’t clear, please let me know. Thanks a lot.
// Initialize the message processing entry
const initializeProcessing = async (s3jsonfile, messageId, totalEntries, messageProcessingTableName) => {
console.log('S3 Key:', s3jsonfile);
console.log('Message Info:', messageId);
console.log('Total Entries:', totalEntries);
const params = {
TableName: messageProcessingTableName,
Key: {
s3jsonfile, // Partition key
messageId // Sort key
},
UpdateExpression: 'SET totalEntries = :totalEntries, entriesProcessed = :entriesProcessed, processingStatus = :processingStatus, lastUpdated = :timestamp',
ExpressionAttributeValues: {
':totalEntries': totalEntries.toString(), // Ensure this is a string
':entriesProcessed': '0', // Plain string
':processingStatus': '0', // Plain string
':timestamp': new Date().toISOString() // ISO string for date
}
};
console.log('Parameters for UpdateItemCommand:', JSON.stringify(params, null, 2)); // Detailed logging
try {
await dynamoDB.send(new UpdateItemCommand(params));
console.log(`Message ${messageId} initialized for ${totalEntries} entries.`);
} catch (error) {
console.error('Error initializing message processing:', error);
console.error('Parameters used:', params); // Log parameters for debugging
}
};
EDIT: To clarify, on this method the entriesProcessed is static as 0 because it will be updated when the processing logic is done. Also, processing status will be updated only when all entries from the json are processed. Thats why their attributes are like static strings now.