0

I am attempting to batchWrite items to my users table, but I am receiving a ValidationException. I can't identify why The provided key element does not match the schema. I created a new table, and the partition key is id.

batchWrite function

const batchWriteUserData = (data) => {
    const input = {
        RequestItems: {
            "users": data.map((item) => ({
                PutRequest: {
                    Item: {
                        id: { S: item.id },
                        name: { S: item.name },
                        username: { S: item.username },
                        email: { S: item.email },
                        password: { S: item.password },
                        blogs: {
                            L: item.blogs.map((blog) => ({
                                M: {
                                    id: { S: blog.id.toString() },
                                    title: { S: blog.title },
                                    content: { S: blog.content },
                                },
                            })),
                        },
                    },
                },
            })),
        },
    }
    console.log(JSON.stringify(input, null, 2))
    dynamoDB.batchWrite(input, (err, data) => {
        if (err) console.error('Error:', err);
        else return 'Sucess';
    });
}

functions that create the data

const generateBlogData = () => {
    const paragraphAmount = Math.floor((Math.random() * 5) + 1);
    const titleAmount = Math.floor((Math.random() * 10) + 1);
    const blogCount = Math.floor(Math.random() * 9 + 1);
    const userBlogs = [];

    for (let i = 0; i < blogCount; i++) {
        const blogTitle = lorem.generateWords(titleAmount);
        const blogContent = lorem.generateParagraphs(paragraphAmount);
        const blogData = {
            id: i + 1,
            title: blogTitle,
            content: blogContent,
        };
        userBlogs.push(blogData);
    };
    return userBlogs;
};

const generateUserData = () => {
    let maxUsers = 100;
    let count = 0;
    let userData = []

    while (maxUsers > 0) {
        const prefix = lorem.generateWords(1);
        const suffix = lorem.generateWords(1);
        const loremPassword = lorem.generateWords(1).concat(Math.floor(Math.random() * 900) + 100);
        
        const data = {
            id: generateUniqueId(),
            name: generateUniqueName(),
            username: generateUniqueUsername(),
            email: `${prefix}@${suffix}.com`,
            password: loremPassword
        };
        data['blogs'] = generateBlogData();

        if (count < 25) {
            userData.push(data);
        }
        else if (count === 25) {
            batchWriteUserData(userData);
        }
        count++;
        maxUsers--;
    }
};

Can someone identify why this is happening? I appreciate any feedback/perspective.

I've tried writing only the user data, even down to just the id to determine if the issues was the blogs data, I also tried storing the blog.id without .toString() and with the type as N but the issue persists.

3
  • Do 2 things and I'll provide an answer. 1. Show how you create your DynamoDB client. 2. Show the output of describe table, or a screenshot of your tables schema (keys) from the console. Commented Nov 19, 2024 at 5:18
  • Hi @LeeroyHannigan, here is how i define my DynamoDB client: import AWS from 'aws-sdk'; AWS.config.update({ accessKeyId: '', secretAccessKey: '', region: '', }); const dynamoDB = new AWS.DynamoDB.DocumentClient(); export default dynamoDB; Commented Nov 19, 2024 at 5:25
  • @LeeroyHannigan I don't have any data in my table at the moment, I started a fresh table to populate with this script. The primary key is id and there is not a sort key. Commented Nov 19, 2024 at 5:27

1 Answer 1

0

You're using the document client which takes native JSON not DDB JSON which you use on your request.

  const input = {
        RequestItems: {
            "users": data.map((item) => ({
                PutRequest: {
                    Item: {
                        id: item.id ,
                        name: item.name ,
                        username: item.username ,
                        email: item.email ,
                        password: item.password ,
                        blogs: 
                           item.blogs.map((blog) => ({
                                    id:  blog.id.toString() ,
                                    title:  blog.title ,
                                    content:  blog.content ,
                            })),
                    },
                },
            })),
        },
    }

Learn more here: https://aws.amazon.com/blogs/database/exploring-amazon-dynamodb-sdk-clients/

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this link and for providing clarification on my issue! This blog is great.
Glad it helped. Feel free to accept the answer if it solved your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.