所以我试图设置一个基本的POST到API Post方法我把它连接到一个简单的dynamodb。我在下面有以下身体映射模板:
{
"TableName": "bars",
"Item": {
"barid": {
"S": "$input.path('$.barid')"
},
"phone": {
"S": "$input.path('$.phone')"
},
"location": {
"S": "$input.path('$.location')"
},
"happyhour": {
"L": [
{
"M": {
"Time": {
"S": "$input.path('$.Time')"
},
"Deal": {
"S": "$input.path('$.Deal')"
},
"Day": {
"S": "$input.path('$.Day')"
}
}
},
{
"M": {
"Time": {
"S": "$input.path('$.Time')"
},
"Deal": {
"S": "$input.path('$.Deal')"
},
"Day": {
"S": "$input.path('$.Day')"
}
}
},
{
"M": {
"Time": {
"S": "$input.path('$.Time')"
},
"Deal": {
"S": "$input.path('$.Deal')"
},
"Day": {
"S": "$input.path('$.Day')"
}
}
},
{
"M": {
"Time": {
"S": "$input.path('$.Time')"
},
"Deal": {
"S": "$input.path('$.Deal')"
},
"Day": {
"S": "$input.path('$.Day')"
}
}
},
{
"M": {
"Time": {
"S": "$input.path('$.Time')"
},
"Deal": {
"S": "$input.path('$.Deal')"
},
"Day": {
"S": "$input.path('$.Day')"
}
}
},
{
"M": {
"Time": {
"S": "$input.path('$.Time')"
},
"Deal": {
"S": "$input.path('$.Deal')"
},
"Day": {
"S": "$input.path('$.Day')"
}
}
},
{
"M": {
"Time": {
"S": "$input.path('$.Time')"
},
"Deal": {
"S": "$input.path('$.Deal')"
},
"Day": {
"S": "$input.path('$.Day')"
}
}
}
]
},
"name": {
"S": "$input.path('$.name')"
}
}
}
以下是我用post:
测试post语句的内容{
"barid": {
"S": "005"
},
"happyhour": {
"L": [{
"M": {
"Time": {
"S": "11AM-9AM"
},
"Deal": {
"S": "$3 Mimosas; $3 Bloody Marys"
},
"Day": {
"S": "Sunday"
}
}
}, {
"M": {
"Time": {
"S": "4PM - 9PM"
},
"Deal": {
"S": "$4 Margaritas, Corona, Corona Light; $3 Bud Light Lime"
},
"Day": {
"S": "Monday"
}
}
}, {
"M": {
"Time": {
"S": "4PM-9PM"
},
"Deal": {
"S": "1/2 Price Burgers; $2 Bud and Bud Light Drafts"
},
"Day": {
"S": "Tuesday"
}
}
}, {
"M": {
"Time": {
"S": "4PM-9PM"
},
"Deal": {
"S": "$2 Drafts; $3 Food Menu"
},
"Day": {
"S": "Wednesday"
}
}
}, {
"M": {
"Time": {
"S": "4PM-9PM"
},
"Deal": {
"S": "$2 Beers, House Liquor, Flavored Vodka; 1/2 Price Wings; $5 Pizzas or Pasta Bowl"
},
"Day": {
"S": "Thursday"
}
}
}, {
"M": {
"Time": {
"S": "4PM-9PM"
},
"Deal": {
"S": "$5 Red Bull House Liquor Cocktails; $5 Wings, Nachos, Sliders, Flatbreads; $5 Finlandia Cocktails and Martinis; $15 Sam Adams Light Buckets"
},
"Day": {
"S": "Friday"
}
}
}, {
"M": {
"Time": {
"S": "None"
},
"Deal": {
"S": "None"
},
"Day": {
"S": "Sunday"
}
}
}]
},
"phone": {
"S": "(703) 527-1600"
},
"location": {
"S": "3100 Clarendon Blvd, Arlington, VA 22201"
},
"name": {
"S": "Mister Days"
}
}
每当我运行它时,似乎它会填充barid,phone和location字段,但会将happyhour数组留空。以下是我在运行测试后在日志中得到的内容。
"TableName": "bars",
"Item": {
"barid": {
"S": "{S=005}"
},
"phone": {
"S": "{S=(703) 527-1576}"
},
"location": {
"S": "{S=2500 Hess Road}"
},
"happyhour": {
"L": [
{
"M": {
"Time": {
"S": ""
},
"Deal": {
"S": ""
},
"Day": {
"S": ""
}
}
},
{
"M": {
"Time": {
"S": ""
},
"Deal": {
"S": ""
},
"Day": {
"S": ""
}
}
},
{
"M": {
"Time": {
"S": ""
},
"Deal": {
"S": ""
},
"Day": {
"S": ""
}
[TRUNCATED]
Thu Jun 30 15:36:27 UTC 2016 : Endpoint response body before transformations: {"__type":"com.amazon.coral.service#UnknownOperationException"}
不确定我做错了什么,但任何事情都会有所帮助。谢谢!
答案 0 :(得分:1)
看起来happyhour包含一个具有名为L的单个属性的对象,该属性包含一个对象数组。您可能希望循环遍历数组,以便无论数组的长度如何,映射模板仍然可以工作。您还需要按照完整路径引用输入中的项目,而不仅仅是名称。
应该看起来像这样......
#set($inputRoot = $input.path('$'))
{
"TableName": "bars",
"Item": {
"barid": {
"S": "$inputRoot.barid.S"
},
"name": {
"S": "$inputRoot.name.S"
},
"phone": {
"S": "$inputRoot.phone.S"
},
"location": {
"S": "$inputRoot.location.S"
},
"happyhour": {
"L": [
#foreach($elem in $inputRoot.happyhour.L)
{
"M": {
"Time": {
"S": "$elem.M.Time.S"
},
"Deal": {
"S": "$elem.M.Deal.S"
},
"Day": {
"S": "$elem.M.Day.S"
}
}
}#if($foreach.hasNext),#end
#end
]
}
}
请注意,映射模板顺序和输出顺序不必与输入顺序匹配。此外,您不必保持完全相同的结构。例如,您可以删除所有额外类型信息并输出更清晰的json ...
#set($inputRoot = $input.path('$'))
{
"TableName": "bars",
"Item": {
"barid": "$inputRoot.barid.S",
"name": "$inputRoot.name.S",
"phone": "$inputRoot.phone.S",
"location": "$inputRoot.location.S",
"happyhour": [
#foreach($elem in $inputRoot.happyhour.L)
{
"Time": "$elem.M.Time.S",
"Deal": "$elem.M.Deal.S",
"Day": "$elem.M.Day.S"
}#if($foreach.hasNext),#end
#end
]
}
答案 1 :(得分:0)
如果您真的想在API中进行映射,这可能不是正确的方法,但您可以将问题留给aws-sdk。
您可以在API和dynamoDB之间创建一个简单的lambda函数。使用Aws提供的一些方法: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html
我用这个函数解决了类似的问题:
var AWS = require("aws-sdk");
var docClient = new AWS.DynamoDB.DocumentClient();
var tableName = "tableName";
var saveData = function (data) {
var params = {
TableName: tableName,
Item: data
};
docClient.put(params, function (err, data) {
if (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Added item:", JSON.stringify(data, null, 2));
}
});
};
exports.handler = function (event) {
saveData(event);
};