简体   繁体   English

如何使用异步等待使 AWS dynamo db 在 nodejs 中工作

[英]how to make AWS dynamo db work in nodejs in using async await

I have this sample dynamo db create data code我有这个示例发电机数据库创建数据代码

var AWS = require("aws-sdk");
let awsConfig = {
    "region": "ap-south-1",
    "endpoint": "http://dynamodb.ap-south-1.amazonaws.com",
    "accessKeyId": "xxxxxxxxxxxxxxxxx", "secretAccessKey": "xxxxxxxxxxxxxxxxxxxxxxx"
};
AWS.config.update(awsConfig);

let docClient = new AWS.DynamoDB.DocumentClient();

console.log(docClient);

let save = function () {

    var input = {
        "task": "example-1@gmail.com"
    };
    var params = {
        TableName: "todos",
        Item:  input
    };
    docClient.put(params, function (err, data) {

        if (err) {
            console.log("error - " + JSON.stringify(err, null, 2));                      
        } else {
            console.log(data);
        }
    });
}



save();

Now I converted this into async-await form现在我将其转换为异步等待形式

const create = async (docClient,payload,tableName) =>{
    try {
        console.log(payload);
        const createdDoc = await docClient.put({TableName:tableName,Item:payload})
        console.log(createdDoc);
    } catch (error) {
       console.log(error); 
    }
}
        


create(docClient,{"task":"sample"},"todos")

This code neither gave me an error neither it add data into dynamo DB tables这段代码既没有给我一个错误,也没有将数据添加到发电机数据库表中

You have to call .promise() on the result of .put() call:您必须在.put() .promise()

const createdDoc = await docClient.put({ … }).promise();

See docs for AWS.DynamoDB.DocumentClient.put() and AWS.Request.promise() .请参阅AWS.DynamoDB.DocumentClient.put()AWS.Request.promise()的文档。

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

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