无法通过Lambda函数访问AWS DynamoDb。
Lambda函数:
'use strict';
const AWS = require('aws-sdk');
AWS.config.update({region: 'ap-southeast-2'});
const getUser = async function () {
console.log('getUser() invoked');
const documentClient = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: 'Users',
Key: {
"UserId": "A001"
}
};
console.log('params ', params);
documentClient.get(params, function (err, data) {
console.log('============>');
if (err) {
console.log('error ', err);
return err;
}
console.log('success ', data);
return data;
});
};
exports.handler = async (event, context, callback) => {
// TODO implement
console.log('event ', event);
const data = await getUser();
console.log('=============> data ', data);
};
在测试Lambda函数时,正在打印的数据不确定。它是打印参数,而不是打印错误和成功。
答案 0 :(得分:0)
您在这里对回调和Promise感到有些困惑,现在的方式是不将数据传递到外部。
const getUser = async function () {
console.log('getUser() invoked');
const documentClient = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: 'Users',
Key: {
"UserId": "A001"
}
};
console.log('params ', params);
try {
const data = await documentClient.get(params).promise();
console.log('============>');
console.log('success ', data);
return data;
} catch(err) {
console.log('error ', err);
throw err;
}
};