【发布时间】:2018-07-14 21:36:24
【问题描述】:
以下代码在 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/await 用法的一些内容,但不知道是什么。使用 nodejs 8.1 和 lambda 函数将项目写入 DynamoDB 的正确方法是什么?
【问题讨论】:
-
您是否尝试检查
err的值以查看问题所在?
标签: node.js amazon-web-services aws-lambda amazon-dynamodb