Shi*_*ade 3 javascript amazon-dynamodb
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = async (event) => {
var note = {};
note.noteid = new Date().getTime();
note.content = event.queryStringParameters["content"];
var res = {};
const response = {
statusCode: 200,
body: JSON.stringify(note),
};
var obj = {
'TableName':'notes',
'Item': {
'note_id': {
S: '2'
},
'name': {
S: 'content'
}
},
'ReturnConsumedCapacity': "TOTAL"
};
dynamodb.putItem(obj, function(err,result){
console.log('function called!!');
console.log(err);
return response;
});
};
Run Code Online (Sandbox Code Playgroud)
我putItem无法正常工作,未调用回调函数。我已经获得了对该用户角色的完全访问权限,但仍未调用函数。
假设您正在使用AWS Lambda。由于您使用的是async/ await模式,因此http响应最终async (event) => {}将返回。就您而言,那不算什么。您打了电话,putItem但没有等待。async (event) => {}此后立即不返回任何内容。由于函数已返回,因此您的putItem调用没有回调的机会。
您应该转换putItem调用promise和await它。然后处理结果并返回http响应。
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = async (event) => {
var note = {};
note.noteid = new Date().getTime();
note.content = event.queryStringParameters["content"];
var res = {};
const response = {
statusCode: 200,
body: JSON.stringify(note),
};
var obj = {
'TableName':'notes',
'Item': {
'note_id': {
S: '2'
},
'name': {
S: 'content'
}
},
'ReturnConsumedCapacity': "TOTAL"
};
try
{
var result = await dynamodb.putItem(obj).promise();
//Handle your result here!
}
catch(err)
{
console.log(err);
}
return response;
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
796 次 |
| 最近记录: |