You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lambda写入DynamoDB报错:参数无法转换为数值类型问题求助

问题根源

你代码中所有读取event对象属性的表达式都被额外的双引号包裹,JavaScript会将其识别为固定字符串字面量,而非读取event对象对应的属性值。报错信息中明确提示无法转换为数值的内容就是event.profileArray[0].profileId这个字符串本身,而非你预期的入参中该字段的实际值2591184040605621。

你代码中除了validPaymentMethod字段的赋值是正确的(没有包裹多余引号,会直接读取变量值),其余所有S/N类型字段的赋值都存在同样的错误,写入DynamoDB的都会是固定的表达式字符串,而非实际入参数据。

修正方案

移除所有变量表达式外侧多余的双引号即可,修改后的params配置参考如下:

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 },
  },
};
验证方法

你可以在定义params之后添加一行日志打印console.log(params.Item.profile_id),修改前会输出{ N: 'event.profileArray[0].profileId' },修改后会输出{ N: '2591184040605621' },即可验证问题已修复。

内容的提问来源于stack exchange,提问作者NickC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 07:00:01