Lambda 和 DynamoDB:"The parameter cannot be converted to a numeric value"

Lambda & DynamoDB: "The parameter cannot be converted to a numeric value"

谁能知道为什么这个数字字符串不能转换成数字!!

我有以下 Lambda 函数,它正在获取一组从 API 网关发送的数据,然后配置它以将其保存到 DynamoDB。

// Load the AWS SDK for Node.js.
var AWS = require("aws-sdk");
// Set the AWS Region.
AWS.config.update({ region: "us-east-1" });

// Create DynamoDB service object.
var ddb = new AWS.DynamoDB({ apiVersion: "2012-08-10" });

exports.handler = async (event) => {
  // response variables
  let body;
  let statusCode = "200";
  const headers = {
    "Content-Type": "application/json",
  };
  // request parameters
  let params = {
    TableName: "users_amazon_profiles",
    Item: {
      "user_id": { "S": "event.user_id" },
      "refresh_token": { "S": "event.refreshToken" },
      "profile_id": { "N": "event.profileArray[0].profileId" },
      "country_code": { "S": "event.profileArray[0].countryCode" },
      "currency_code": { "S": "event.profileArray[0].currencyCode" },
      "account_info": { 
        "M": {
          "marketplaceStringId" : { "S": "event.profileArray[0].accountInfo.marketplaceStringId" },
          "id" : { "S": "event.profileArray[0].accountInfo.id" },
          "name" : { "S": "event.profileArray[0].accountInfo.name" },
          "type" : { "S": "event.profileArray[0].accountInfo.type" },
          "validPaymentMethod" : { "BOOL": event.profileArray[0].accountInfo.validPaymentMethod }
        }
      },
      "daily_budget": { "N": "event.profileArray[0].dailyBudget" },
      "timezone": { "S": "event.profileArray[0].timezone" },
    },
  };
  
//  request
  try {
    console.log("profileId:");
    console.log(event.profileArray[0].profileId);
    body = await ddb.putItem(params).promise();
  } catch (err) {
    statusCode = "400";
    body = err.message;
    
  }

//  dynamic response
  return {
    statusCode,
    body,
    headers,
  };
};

这是用于测试的 JSON 测试对象:

{
  "user_id": "g490754-49b9-9079-c63aba5cccf6",
  "refreshToken": "Atzr|IwEBIMvPPWsTFHLgvbEhgUn30P57Kake_nfIgVa-6GJeIe9vsW38GdoDcpzCXIlqd6-82YdC8JISNxuQJQ5QREGmSX0YRu6qJ4zteznkqoaygCPk1ZqHE2jPhcbDuDZiGRNOWpis0pPpKaS9ylJsp2Vquwa5lM6Olu_qhRanp8iewCLmK-k4W8FkjA0aiiCqJPyfb7dsloXO-TGDPK8WXSoLREnzhucaO5Xn-TnwicZzKrq0mYBHyhzm1XNvEGmSiDm1cxkgFcQr0TvkVhyOoTeLuKz3Cw-1iAQg6rbaXiEvgo18ganZYO71vpv4jy5S91h5NVZwCgtG7tJ73A5DzOBdRXjjhke5xJ7HHEZQncQUPjE_R0gZBtsXpKfJVaaPjY925m-GCMbdhe9aFR0gKYlaDz-q-Nbo_I-vUA",
  "profileArray": [
    {
      "accountInfo": {
        "marketplaceStringId": "OKE9KIKX0DER",
        "id": "OI9FU2GLDTTWKS",
        "name": "Test Seller",
        "type": "seller",
        "validPaymentMethod": true
      },
      "countryCode": "US",
      "currencyCode": "USD",
      "dailyBudget": "999999999",
      "profileId": "2591184040605621",
      "timezone": "America/Los_Angeles"
    },
    {
      "accountInfo": {
        "marketplaceStringId": "ATVERKIKX0DER",
        "id": "A3UJTRELDTTWKS",
        "name": "ACME",
        "type": "vendor",
        "validPaymentMethod": false
      },
      "countryCode": "US",
      "currencyCode": "USD",
      "dailyBudget": "10000",
      "profileId": "999918498705620",
      "timezone": "America/Los_Angeles"
    },
    {
      "accountInfo": {
        "marketplaceStringId": "ATVOIKIKX0DER",
        "id": "A3UJU6HLDTTWKS",
        "name": "Woolworths",
        "type": "seller",
        "validPaymentMethod": true
      },
      "countryCode": "US",
      "currencyCode": "USD",
      "dailyBudget": "999999999",
      "profileId": "111184040605621",
      "timezone": "America/Los_Angeles"
    }
  ]
}

所以基本上在 Lambda 函数中,我们正在查看其中包含三个对象的 profileArray,我们从第一个对象中取出数据并将其放入我们要访问的参数中保存到 DynamoDB。

我们遇到问题的具体项目是: "profile_id": { "N": "event.profileArray[0].profileId" },

我们可以看到我们将这段数据指定为一个数字,我们将它作为一个字符串输入(我们应该这样做)并且在测试 JSON 对象中,我们可以看到它是一个字符串。但是,当我们 运行 这个函数时,我们得到响应:

{
  "statusCode": "400",
  "body": "The parameter cannot be converted to a numeric value: event.profileArray[0].profileId",
  "headers": {
    "Content-Type": "application/json"
  }
}

这太疯狂了,因为那项绝对是一个数字。我已经尝试了我能想到的一切。您可以在请求中看到我控制台记录了该项目只是为了确保它正确进入,是的,我们在控制台日志中得到以下内容:

2591184040605621

我认为可能是问题的一件事是 profile_id 是 DynamoDB table 的排序键,所以我确保此排序键的数据类型是数字,所以这不是问题。

谁能知道为什么这个数字字符串不能转换成数字!!

无法转换,因为您实际上是将其作为字符串传递。

从引用 event.

的 JSON 对象中删除 profileId 周围的引号 (")
...
"profile_id": { "N": event.profileArray[0].profileId }
...