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

TypeScript操作DynamoDB报错:AttributeValue类型不存在split属性求助

问题原因
  • DynamoDB返回的所有字段都是AttributeValue类型的包装对象,而非原生数据类型。你的newImage.type实际结构是类似{ S: "Leave#Upskilling" }的对象,不是字符串,自然不存在split方法,这是报错的核心原因。
  • 你的代码逻辑顺序错误:应该先调用getAttributeValue工具函数拿到type字段的原生字符串值,再对字符串做split拆分,而非对AttributeValue对象直接拆分后再传参给getAttributeValue。
  • 原代码还有语法错误:getAttributeValue(newImage.type.split('#')[0]))末尾多了一个多余的右括号,会触发语法报错。
解决方案

调整代码逻辑,先提取type的字符串值再拆分即可,参考代码如下:

// 先通过工具函数拿到type字段的字符串值
const typeRaw = getAttributeValue(newImage.type);
// 拆分字符串,增加兜底逻辑避免无#分隔符时的报错
const [mainType, subLeaveType = ''] = typeRaw.split('#');

recordsToJoin.push(
  getAttributeValue(newImage.id),
  getAttributeValue(newImage.days), 
  getAttributeValue(newImage.end),
  getAttributeValue(newImage.createdAt, true),
  mainType,
  subLeaveType
);

如果需要更强的健壮性,可以补充拆分长度校验:

const typeParts = typeRaw.split('#');
const mainType = typeParts[0] || '';
const subLeaveType = typeParts.length >= 2 ? typeParts[1] : '';

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 06:27:02