简体   繁体   English

如何修复数据未插入本地 dynamodb (NodeJS)

[英]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.我正在尝试通过我创建的端点将数据插入本地 dynamoDB,但它不起作用。 There were no error log in the promise itself and it seems like the function is being ignored. promise 本身没有错误日志,似乎 function 被忽略了。

This is a helper function that will assist in inserting the data这是一个 helper function 将协助插入数据

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这是我调用上面的 function 并为其提供要插入的表名的部分

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预期:如果执行 put function,将会有一个控制台日志记录成功并将数据插入到表中,如果有错误也会记录错误

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"我通过将 accessKeyId 和 secretAccessKey 添加到“AWS.DynamoDB.DocumentClient”的配置选项来解决它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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