此功能未同步运行。因此,在创建数据库之前,它试图将数据放入表中。

问题描述 投票:1回答:1
app.get('/:table_name',function createDatabase(req,res){ AWS.config.update({ region: "", endpoint: "http://localhost:8000", accessKeyId :"", secretAccessKey: "" });
我知道我可以使用诺言,异步等待,但是我不知道如何在我的代码中使用它们。有人可以帮我吗?我希望它在调用此函数时先创建一个数据库,然后再将其放入数据库中。请帮助!

var dynamodb = new AWS.DynamoDB(); var params11 = { TableName : "Movies", // TableName : req.params.table_name, KeySchema: [ { AttributeName: "year", KeyType: "HASH"}, //Partition key { AttributeName: "title", KeyType: "RANGE" } //Sort key ], AttributeDefinitions: [ { AttributeName: "year", AttributeType: "N" }, { AttributeName: "title", AttributeType: "S" } ], ProvisionedThroughput: { ReadCapacityUnits: 10, WriteCapacityUnits: 10 } }; dynamodb.createTable(params11, function(err, data) { if (err) { console.log("Unable to create table. Error JSON:", JSON.stringify(err, null, 2)); } else { console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2)); } }); console.log("Importing movies into DynamoDB. Please wait."); var allMovies = JSON.parse(fs.readFileSync('./moviedata.json', 'utf8')); allMovies.forEach(function(movie) { param3 = { TableName: req.params.table_name, Item: { "year": movie.year, "title": movie.title, "info": movie.info } }; docClient.put(param3, function(err, data) { if (err) { console.error("Unable to add movie", movie.title, ". Error JSON:", JSON.stringify(err, null, 2)); } else { console.log("PutItem succeeded:", movie.title); } }); }); res.send("DONE") })

app.get('/:table_name',function createDatabase(req,res){AWS.config.update({region:“”,端点:“ http:// localhost:8000”,accessKeyId:“”,secretAccessKey :“”});我知道我可以使用promises,...
node.js promise callback async-await amazon-dynamodb
1个回答
0
投票
我认为您从根本上误解了异步功能如何在Javascript / Nodejs中工作。
© www.soinside.com 2019 - 2024. All rights reserved.