2016-06-30 15 views
0

Also ich versuche, eine grundlegende POST zu einer API-Post-Methode, die ich gemacht, die mit einem einfachen Dynamob verbunden ist. Ich habe folgende Body Mapping-Vorlage unter:Body Mapping Template + API-Gateway

{ 
"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')" 
    } 
} 
} 

Hier ist, was ich die Prüfung der Post-Anweisung mit:

{ 
"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" 
} 

}

Jedes Mal, wenn ich das laufen scheint es, dass es die auffüllt Barid-, Telefon- und Standortfeld, aber das happyhour-Feld bleibt leer. Unten ist, was ich in den Protokollen nach dem Test bekommen habe.

"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"} 

Nicht sicher, was ich falsch mache, aber irgendetwas würde helfen. Vielen Dank!

Antwort

0

Es sieht so aus, als ob happyhour ein Objekt mit einem einzelnen Attribut namens L enthält, das ein Array von Objekten enthält. Wahrscheinlich möchten Sie das Array durchlaufen, damit Ihre Mapping-Vorlage unabhängig von der Länge des Arrays funktioniert. Sie müssen Elemente in der Eingabe auch anhand ihres vollständigen Pfads und nicht nur ihres Namens referenzieren.

Es wie folgt aussehen sollte ...

#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 
    ] 
    } 
} 

Beachten Sie, dass die Zuordnungsvorlage Ordnung und die Ausgabereihenfolge müssen nicht die Eingabereihenfolge entsprechen. Außerdem müssen Sie nicht genau die gleiche Struktur beibehalten. Sie könnten zum Beispiel so alle zusätzlichen Typinformation und Ausgabereiniger json entfernen ...

#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 
    ] 
} 
+0

ich Ihre zweite Teil versucht laufen, die Sie vorgeschlagen, aber aus irgendeinem Grund es gab mir noch eine Serialisierung Fehler –

+0

in den Protokollen es alles korrekt zugeordnet, aber aus irgendeinem Grund gibt es immer noch den Serialisierungsfehler –

0

Das ist nicht richtig Ansatz sein könnte, wenn Sie wirklich die Zuordnung in der API tun wollen, aber Sie können überlasse das Problem aws-sdk.

Sie können eine einfache Lambda-Funktion zwischen Ihrer API und dynamoDB erstellen. Verwenden Sie einige der von Aws bereitgestellt Methoden: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html

in Angriff genommen ich ein ähnliches Problem mit dieser Funktion:

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); 
};