3

I am trying to insert data into local dynamoDB through a endpoint I created but it is not working. There were no error log in the promise itself and it seems like the function is being ignored.

This is a helper function that will assist in inserting the data

require('aws-sdk/index');
const DynamoDB = require('aws-sdk/clients/dynamodb');

const options = {
    apiVersion: '2012-08-10',
    region: 'ap-southeast-1',
    endpoint: 'http://localhost:8000/',
};

const dynamoDBDoc = new DynamoDB.DocumentClient(options);

async function putData(tableName,insertValue) {
    const param = {
        TableName: tableName,
        Item:insertValue,
        ReturnConsumedCapacity: 'TOTAL',
    };

    // Mock data
    // const param = {
    //     TableName: 'user',
    //     Item:{
    //         'user_id':'1234',
    //         'email':'memelorde'
    //     },
    //     ReturnConsumedCapacity: 'TOTAL',
    // };

    try {
        const data = await dynamoDBDoc.put(param).promise();
        console.log("Data successfully entered", data)
        return data;
    } catch (e) {
        console.log(`failed ${e.message}`);
        return false;
    }
}

This is the part where I call the above function and provide it with the table name to insert into

async function createUser(data){
    const tableName = "user"
    data["user_id"]= uuidv4() 
    await dynamoDB.putData("user",data);

    return await dynamoDB.putData(tableName, data);
}

This is the endpoint I created to pass in user information

if (event.httpMethod === 'POST') {
     dataset = JSON.parse(event.body)
     console.log('/signup POST req =>', dataset)
     let res = await user.createUser(dataset)
     console.log(res)               
}

Expected: If the put function executed, there will be a console log that logs the success and the data will be inserted to the table, if there is an error the error will be logged too

Actual: No error code was produced at all

7
  • Seen fine. Why do you know data not be insert to user table on DynamoDB local? Commented Apr 10, 2019 at 9:04
  • const data = await dynamoDBDoc.put(param).promise(); data will be like {}, put method does not return any thinks. You can use return param.Items instead of return data if you want to return new item info. Commented Apr 10, 2019 at 9:07
  • @hoangdv I check the table through this:[hub.docker.com/r/yamitzky/dynamodb-gui/] it connects to the localhost and display the existing tables and information pertaining to it Commented Apr 10, 2019 at 9:18
  • Did you see user table? Commented Apr 10, 2019 at 10:10
  • yes i did that is why i knew it was not working Commented Apr 12, 2019 at 1:01

1 Answer 1

1

I was facing the same issue. I solved it by adding accessKeyId and secretAccessKey to the configuration option of "AWS.DynamoDB.DocumentClient"

const options = {
    accessKeyId: "aaa",
    secretAccessKey: "bbb",
    apiVersion: '2012-08-10',
    region: 'ap-southeast-1',
    endpoint: 'http://localhost:8000/',
};
Sign up to request clarification or add additional context in comments.

Comments

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.