Lambda函数testing永远不会通过

我试图让我的第一个Lambda函数工作,但我不能得到一个testing写入我的dynamoDB。 到目前为止,我一直在关注AWS教程。

这是我的Lambda函数(从教程略微修改,以适应我的数据库):

'use strict'; console.log('Loading function'); const doc = require('dynamodb-doc'); const dynamo = new doc.DynamoDB(); /** * Provide an event that contains the following keys: * * - operation: one of the operations in the switch statement below * - tableName: required for operations that interact with DynamoDB * - payload: a parameter to pass to the operation being performed */ exports.handler = (event, context, callback) => { console.log('Received event:', JSON.stringify(event, null, 2)); console.log('\nPayload:', event.Item); console.log('event table', event.TableName); console.log('\nEvent:', event); const operation = event.operation; const payload = event.Item; // if (event.tableName) { // payload.TableName = event.tableName; // } switch (operation) { case 'create': dynamo.putItem(payload, callback); break; case 'read': dynamo.getItem(payload, callback); break; case 'update': dynamo.updateItem(payload, callback); break; case 'delete': dynamo.deleteItem(payload, callback); break; case 'list': dynamo.scan(payload, callback); break; case 'echo': callback(null, payload); break; case 'ping': callback(null, 'pong'); break; default: callback(new Error(`Unrecognized operation "${operation}"`)); } }; 

这是我的testing有效载荷:

 { "TableName": "EVENTS", "operation": "create", "Item" : { "userID": "2", "kidNo": 2, "note": "Testing", "timeStamp": "timer1" } } 

这是我得到的答复之一:

 { "errorMessage": "There were 6 validation errors:\n* MissingRequiredParameter: Missing required key 'TableName' in params\n* MissingRequiredParameter: Missing required key 'Item' in params\n* UnexpectedParameter: Unexpected key 'userID' found in params\n* UnexpectedParameter: Unexpected key 'kidNo' found in params\n* UnexpectedParameter: Unexpected key 'timeStamp' found in params\n* UnexpectedParameter: Unexpected key 'note' found in params", "errorType": "MultipleValidationErrors", "stackTrace": [ "* MissingRequiredParameter: Missing required key 'TableName' in params", "* MissingRequiredParameter: Missing required key 'Item' in params", "* UnexpectedParameter: Unexpected key 'userID' found in params", "* UnexpectedParameter: Unexpected key 'kidNo' found in params", "* UnexpectedParameter: Unexpected key 'timeStamp' found in params", "* UnexpectedParameter: Unexpected key 'note' found in params", "ParamValidator.validate (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:40:28)", "Request.VALIDATE_PARAMETERS (/var/runtime/node_modules/aws-sdk/lib/event_listeners.js:125:42)", "Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20)", "callNextListener (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:95:12)", "/var/runtime/node_modules/aws-sdk/lib/event_listeners.js:85:9", "finish (/var/runtime/node_modules/aws-sdk/lib/config.js:315:7)", "/var/runtime/node_modules/aws-sdk/lib/config.js:333:9", "EnvironmentCredentials.get (/var/runtime/node_modules/aws-sdk/lib/credentials.js:126:7)", "getAsyncCredentials (/var/runtime/node_modules/aws-sdk/lib/config.js:327:24)", "Config.getCredentials (/var/runtime/node_modules/aws-sdk/lib/config.js:347:9)" ] } 

我明白这是什么告诉我(我想),但我也不是因为我无法修复它! 我已经尝试了错误中的所有build议,并且不会消失。 我觉得我缺less一些基本的东西。

看起来像传递给dynamodb的payload是不正确的,因为payload被剥离到Item中的对象(从你的testing事件)。

 const payload = event.Item; // the value of payload from above line: // payload = { // "userID": "2", // "kidNo": 2, // "note": "Testing", // "timeStamp": "timer1" // } 

一个简单的黑客,你可以尝试

 const payload = { TableName: event.TableName, Item: event.Item }