티스토리 뷰

아래와 같은 코드로 DDB에 값을 넣으려고 했더니
InvalidParameterType: Expected params.Item['StartTime'].N to be a string
라면서 에러가 난다.

async function putBook(bookId, title, password, startTime, endTime) {
  return await ddb.putItem({
    TableName: BOOKS_TABLE_NAME,
    Item: {
      'BookId': { S: bookId },
      'Title': { S: title },
      'Password': { S: password },
      'StartTime': { N: startTime },
      'EndTime': { N: endTime }
    }
  }).promise();
}

Cloudwatch에서 확인한 정확한 에러 내용은 아래와 같다.

{
    "errorType": "MultipleValidationErrors",
    "errorMessage": "There were 2 validation errors:\n* InvalidParameterType: Expected params.Item['StartTime'].N to be a string\n* InvalidParameterType: Expected params.Item['EndTime'].N to be a string",
    "code": "MultipleValidationErrors",
    "message": "There were 2 validation errors:\n* InvalidParameterType: Expected params.Item['StartTime'].N to be a string\n* InvalidParameterType: Expected params.Item['EndTime'].N to be a string",
    "errors": [
        {
            "errorType": "InvalidParameterType",
            "errorMessage": "Expected params.Item['StartTime'].N to be a string",
            "code": "InvalidParameterType",
            "message": "Expected params.Item['StartTime'].N to be a string",
            "time": "2021-01-13T08:06:32.300Z",
            "stack": [
                "InvalidParameterType: Expected params.Item['StartTime'].N to be a string",
                "    at ParamValidator.fail (/var/task/aws-sdk/lib/param_validator.js:50:37)",
                "    at ParamValidator.validateType (/var/task/aws-sdk/lib/param_validator.js:232:10)",
                "    at ParamValidator.validateString (/var/task/aws-sdk/lib/param_validator.js:154:32)",
                "    at ParamValidator.validateScalar (/var/task/aws-sdk/lib/param_validator.js:130:21)",
                "    at ParamValidator.validateMember (/var/task/aws-sdk/lib/param_validator.js:94:21)",
                "    at ParamValidator.validateStructure (/var/task/aws-sdk/lib/param_validator.js:75:14)",
                "    at ParamValidator.validateMember (/var/task/aws-sdk/lib/param_validator.js:88:21)",
                "    at ParamValidator.validateMap (/var/task/aws-sdk/lib/param_validator.js:117:14)",
                "    at ParamValidator.validateMember (/var/task/aws-sdk/lib/param_validator.js:92:21)",
                "    at ParamValidator.validateStructure (/var/task/aws-sdk/lib/param_validator.js:75:14)"
            ]
        },
        {
            "errorType": "InvalidParameterType",
            "errorMessage": "Expected params.Item['EndTime'].N to be a string",
            "code": "InvalidParameterType",
            "message": "Expected params.Item['EndTime'].N to be a string",
            "time": "2021-01-13T08:06:32.300Z",
            "stack": [
                "InvalidParameterType: Expected params.Item['EndTime'].N to be a string",
                "    at ParamValidator.fail (/var/task/aws-sdk/lib/param_validator.js:50:37)",
                "    at ParamValidator.validateType (/var/task/aws-sdk/lib/param_validator.js:232:10)",
                "    at ParamValidator.validateString (/var/task/aws-sdk/lib/param_validator.js:154:32)",
                "    at ParamValidator.validateScalar (/var/task/aws-sdk/lib/param_validator.js:130:21)",
                "    at ParamValidator.validateMember (/var/task/aws-sdk/lib/param_validator.js:94:21)",
                "    at ParamValidator.validateStructure (/var/task/aws-sdk/lib/param_validator.js:75:14)",
                "    at ParamValidator.validateMember (/var/task/aws-sdk/lib/param_validator.js:88:21)",
                "    at ParamValidator.validateMap (/var/task/aws-sdk/lib/param_validator.js:117:14)",
                "    at ParamValidator.validateMember (/var/task/aws-sdk/lib/param_validator.js:92:21)",
                "    at ParamValidator.validateStructure (/var/task/aws-sdk/lib/param_validator.js:75:14)"
            ]
        }
    ],
    "time": "2021-01-13T08:06:32.300Z",
    "stack": [
        "MultipleValidationErrors: There were 2 validation errors:",
        "* InvalidParameterType: Expected params.Item['StartTime'].N to be a string",
        "* InvalidParameterType: Expected params.Item['EndTime'].N to be a string",
        "    at ParamValidator.validate (/var/task/aws-sdk/lib/param_validator.js:40:28)",
        "    at Request.VALIDATE_PARAMETERS (/var/task/aws-sdk/lib/event_listeners.js:126:42)",
        "    at Request.callListeners (/var/task/aws-sdk/lib/sequential_executor.js:106:20)",
        "    at callNextListener (/var/task/aws-sdk/lib/sequential_executor.js:96:12)",
        "    at /var/task/aws-sdk/lib/event_listeners.js:86:9",
        "    at finish (/var/task/aws-sdk/lib/config.js:379:7)",
        "    at /var/task/aws-sdk/lib/config.js:397:9",
        "    at EnvironmentCredentials.get (/var/task/aws-sdk/lib/credentials.js:127:7)",
        "    at getAsyncCredentials (/var/task/aws-sdk/lib/config.js:391:24)",
        "    at Config.getCredentials (/var/task/aws-sdk/lib/config.js:411:9)"
    ]
}

무엇이 문제일까?
DynamoDB의 Number Type에 값을 넣을 땐, 아무리 Number Type이라 해도 String 값을 넣어줘야 한다.
따라서 위의 코드는 아래 처럼 바꾸거나, 애당초 파라미터로 String type을 넘겼어야 했다.

async function putBook(bookId, title, password, startTime, endTime) {
  return await ddb.putItem({
    TableName: BOOKS_TABLE_NAME,
    Item: {
      'BookId': { S: bookId },
      'Title': { S: title },
      'Password': { S: password },
      'StartTime': { N: String(startTime) },
      'EndTime': { N: String(endTime) }
    }
  }).promise();
}
댓글
댓글쓰기 폼