以下代码在nodejs 6.1中工作正常
console.log('Starting function registration');
const AWS = require('aws-sdk');
AWS.config.update({region: 'eu-central-1'});
const docClient = new AWS.DynamoDB({apiVersion: '2012-10-08'});
exports.handler = function(event, context, callback) {
var params = {
'TableName' : 'services',
'Item': {
'name': {S: 'test'},
'phone': {S: '0742324232'},
'available': {BOOL: true}
}
};
docClient.putItem(params, function(err, data) {
if (err) {
console.log("Error", err);
callback(err);
} else {
console.log("Success", data);
callback(data);
}});
};
但是,当尝试在nodejs 8.1样式中使用它时,它不会向数据库中写入任何内容:
console.log('Starting function registration');
const AWS = require('aws-sdk');
AWS.config.update({region: 'eu-central-1'});
const docClient = new AWS.DynamoDB({apiVersion: '2012-10-08'});
exports.handler = async (event, context, callback) => {
var params = {
'TableName' : 'services',
'Item': {
'name': {S: 'test2'},
'phone': {S: '0742324232'},
'available': {BOOL: false}
}
};
var {err, data} = await docClient.putItem(params);
return data;
};
我觉得我遗漏了Async/Wait的一些用法,但是我不知道是什么。使用nodejs 8.1和lambda函数将项目写入dynamodb的正确方法是什么?