【发布时间】:2018-11-27 18:22:28
【问题描述】:
我正在向 Api Gateway 传递一个参数,以使用 AWS Lambda Node JS 将其保存在 DynamoDB 中。
Api Gateway 的集成请求是:
#set($inputRoot = $input.path('$'))
{
"name" : "$inputRoot.name",
"profileImage": "$inputRoot.profileImage",
"gender" : "$inputRoot.gender",
"interests": #foreach ( $item in ["$inputRoot.interests"] ) $item #end,
"surveyOne": "$inputRoot.surveyOne",
"surveyOneAnswer": "$inputRoot.surveyOneAnswer",
"surveyTwo": "$inputRoot.surveyTwo",
"surveyTwoAnswer": "$inputRoot.surveyTwoAnswer"
}
AWS Lambda(Node JS)的内容接收参数并保存到DynamoDB:
const params = {
Item: {
'uuid': { S: "i_" + uuidv4() },
'name': { S: event.name },
'profileImage': { S: event.profileImage },
'gender': { S: event.gender },
'interests': { SS: event.interests },
'surveys' : {
L: [
{
M: {
'type': { S: event.surveyOne },
'answer': { S: event.surveyOneAnswer },
},
M: {
'type': { S: event.surveyTwo },
'answer': { S: event.surveyTwoAnswer }
}
}
]
}
},
TableName: 'users'
};
dynamodb.putItem(params, (err, data) => {
if (err) {
const response = {
statusCode: 500,
headers: {
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({status: false})
};
return callback(null, response);
}
// return status 200 if success
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({status: 'A new record has been added'})
};
return callback(null, response);
})
但 Dynamo DB 中保存的项目仅包含 一项调查,即 surveyTwo。应该是 2 因为我通过了surveyOne 和surveyTwo 输入和回答。
我的预期结果应该是
{
"name": "John Doe",
"profileImage": "https://example.com",
"gender": "m",
"interests": ["Basketball", "Swimming"],
"surveys": [
{ "type": "question 1", "answer": "answer to question 1" },
{ "type": "question 2", "answer": "answer to question 2" }
]
}
【问题讨论】:
标签: amazon-web-services api aws-lambda amazon-dynamodb aws-api-gateway