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

AWS DynamoDB中documentClient.query未找到数据时的返回结果是什么?

DynamoDB查询不存在ID时的返回结果

当你调用documentClient.query().promise()查询一个不存在的ID时,不会触发catch逻辑,而是返回一个结构完整的对象,具体内容如下:

  • Items: 空数组[](没有匹配到任何数据)
  • Count: 0(匹配到的条目数量)
  • ScannedCount: 0(扫描过的条目数量)

顺便提下你代码里的两个问题:

  1. map里直接用await无效,map不会等待异步操作完成,必须用Promise.all()包裹才能正确获取所有查询结果
  2. 你的catch只打印错误但没有返回值,会导致对应ID的结果变成undefined,建议在catch里返回明确的标识,比如带error字段的对象

修正后的代码示例:

const results = await Promise.all(ids.map(async (id) => {
  try {
    return await documentClient.query({
      TableName: 'table-name',
      IndexName: 'by-index-all',
      ProjectionExpression: '....',
      ExpressionAttributeValues: {
        ':id': id,
      },
      KeyConditionExpression: 'id = :id',
    }).promise();
  } catch (e) {
    console.log(`查询ID ${id}失败:`, e);
    return { Items: [], error: e.message };
  }
}));

内容的提问来源于stack exchange,提问作者小熊呆呆Kumaa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 03:47:02