Lambda函数测试永远不会通过

时间:2017-08-24 00:25:18

标签: node.js amazon-web-services lambda aws-lambda

我正在尝试使用我的第一个Lambda函数,但是我无法测试写入我的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}"`));
    }
};

这是我的测试有效载荷:

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

我明白它告诉我的是什么(我认为),但我也不知道,因为我无法解决它!我已经尝试了错误中的所有建议,但它们并没有消失。我觉得我错过了一些基本的东西。

1 个答案:

答案 0 :(得分:2)

传递给dynamodb的有效负载似乎不正确,因为bundle update仅被剥离到Item中的对象(来自测试事件)。

payload

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

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