AWS抱怨参数不是正确的类型,而实际上却不是

时间:2018-10-12 06:29:53

标签: aws-lambda amazon-dynamodb

我是AWS的新手。像新的3天。我在Amazon的教程之一中发现了与DynamoDB表进行交互的lambda函数的代码。该示例仅为GET请求提供测试JSON,这将导致数据库中的项目列表,但代码本身支持PUT,POST和DELETE,以使用表进行完整CRUD。

因此,我创建了一个名为“ Users”的DynamoDB表,它的分区键为 CompanyId ,而分区键为 Email < / em> ,它也是一个字符串。 The Users table

然后我创建了一个lambda函数并添加了教程中的代码:

console.log('Loading function');

const doc = require('dynamodb-doc');

const dynamo = new doc.DynamoDB();

/**
 * Demonstrates a simple HTTP endpoint using API Gateway. You have full
 * access to the request and response payload, including headers and
 * status code.
 *
 * To scan a DynamoDB table, make a GET request with the TableName as a
 * query string parameter. To put, update, or delete an item, make a POST,
 * PUT, or DELETE request respectively, passing in the payload to the
 * DynamoDB API as a JSON body.
 */
exports.handler = (event, context, callback) => {
    console.log('Received event:', JSON.stringify(event, null, 2));

    const done = (err, res) => callback(null, {
        statusCode: err ? '400' : '200',
        body: err ? err.message : JSON.stringify(res),
        headers: {
            'Content-Type': 'application/json',
        },
    });

    switch (event.httpMethod) {
        case 'DELETE':
            dynamo.deleteItem(JSON.parse(event.body), done);
            break;
        case 'GET':
            dynamo.scan({ TableName: event.queryStringParameters.TableName }, done);
            break;
        case 'POST':
            console.log(event.body);
            dynamo.putItem(JSON.parse(event.body), done);
            break;
        case 'PUT':
            dynamo.updateItem(JSON.parse(event.body), done);
            break;
        default:
            done(new Error(`Unsupported method "${event.httpMethod}"`));
    }
};

在测试GET代码(并获得零长度的项目列表,因为我的表为空)之后,我决定创建一个测试,将一个项目添加到数据库中。我创建了一个名为PostTest的测试事件,其测试内容如下:

{
  "httpMethod": "POST",
  "body": "{\"Item\": {\"CompanyId\": {\"S\": \"test-company-id\"},\"FirstName\": {\"S\": \"John\"}, \"LastName\": {\"S\": \"Doe\"}, \"Email\": {\"S\": \"john.doe@gmail.com\"}, \"Password\": {\"S\": \"password\"}}, \"TableName\": \"Users\"}"
}

我想指出一个事实,即 CompanyId 被设置为“ S”,或者换句话说,被设置为字符串类型。

运行测试时,得到以下响应:

{
  "statusCode": "400",
  "body": "One or more parameter values were invalid: Type mismatch for key CompanyId expected: S actual: M",
  "headers": {
    "Content-Type": "application/json"
  }
}

,日志内容为:

START RequestId: 294a7414-cde5-11e8-a7bd-bd58483e1612 Version: $LATEST
2018-10-12T06:07:56.232Z    294a7414-cde5-11e8-a7bd-bd58483e1612    Received event: {
  "httpMethod": "POST",
  "body": "{\"Item\": {\"CompanyId\": {\"S\": \"test-company-id\"},\"FirstName\": {\"S\": \"John\"}, \"LastName\": {\"S\": \"Doe\"}, \"Email\": {\"S\": \"john.doe@gmail.com\"}, \"Password\": {\"S\": \"password\"}}, \"TableName\": \"Users\"}"
}
2018-10-12T06:07:56.232Z    294a7414-cde5-11e8-a7bd-bd58483e1612    {"Item": {"CompanyId": {"S": "test-company-id"},"FirstName": {"S": "John"}, "LastName": {"S": "Doe"}, "Email": {"S": "john.doe@gmail.com"}, "Password": {"S": "password"}}, "TableName": "Users"}
END RequestId: 294a7414-cde5-11e8-a7bd-bd58483e1612
REPORT RequestId: 294a7414-cde5-11e8-a7bd-bd58483e1612  Duration: 173.04 ms Billed Duration: 200 ms     Memory Size: 512 MB Max Memory Used: 30 MB

我对响应中的错误感到困惑:

Type mismatch for key CompanyId expected: S actual: M

如果您查看日志,则会在请求的正文中将 CompanyId 设置为“ S”。 “ M”来自哪里?我做错了什么?

不用说,我的表中没有添加任何项目。

1 个答案:

答案 0 :(得分:0)

我不知道dynamodb-doc的背后是什么,但是我对aws-sdk也有类似的问题。

给定const AWS = require('aws-sdk'),您可能有两个连接器。示例:

  • const DDB = new AWS.DynamoDB();
  • const DC = new AWS.DynamoDB.DocumentClient();

使用DDB连接时,必须使用类型来格式化项目。

使用DocumentClient(DC)时会推断类型,因此您不必显式提供类型。尝试这样的事情:

{
  "httpMethod": "POST",
  "body": "{\"Item\": {\"CompanyId\": \"test-company-id\",\"FirstName\": \"John\", \"LastName\": \"Doe\", \"Email\": \"john.doe@gmail.com\", \"Password\":  \"password\"}, \"TableName\": \"Users\"}"
}

使用DC,可以正确地将CompanyId推断为Map:{\"S\": \"test-company-id\"}。垃圾进垃圾出。 :)