简体   繁体   中英

How to fix data not inserting into local dynamodb (NodeJS)

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

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/',
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM