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

Node Express调用AWS SDK扫描DynamoDB触发Request.HTTP_DATA报错如何解决

问题复现

正在搭建可访问AWS DynamoDB的Node Express API,学习async/await用法时运行代码报错,错误类型为TypeError,提示无法读取undefined的push属性,关联Request.HTTP_DATA事件。

问题代码

app.get("/vibefinder-photos", asyncWrapper(async (req) => {
  console.log('getting photos')
  let output = await getVibefinderPhotos();
  return output;
}));

async function getVibefinderPhotos() {
  const params = {
    TableName: "vibefinder-photos",
  };
  try {
    const r = await dynamodb.scan(params, function (err, data) {
      if (err) {
        console.log("Error scan ", err);
      } else {
        console.log(data)
      }
    }).promise();
    return r;
  } catch (e) {
    console.log(e);
  }
}

完整报错堆栈

Error scan  Error [TypeError]: Cannot read property 'push' of undefined
    at Request.HTTP_DATA (/Users/nickwoods/Documents/GitHub/nodeapi/node_modules/aws-sdk/lib/event_listeners.js:418:35)
    at Request.callListeners (/Users/nickwoods/Documents/GitHub/nodeapi/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/Users/nickwoods/Documents/GitHub/nodeapi/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/Users/nickwoods/Documents/GitHub/nodeapi/node_modules/aws-sdk/lib/request.js:688:14)
    at IncomingMessage.onReadable (/Users/nickwoods/Documents/GitHub/nodeapi/node_modules/aws-sdk/lib/event_listeners.js:318:32)
    at IncomingMessage.emit (node:events:394:28)
    at IncomingMessage.emit (node:domain:470:12)
    at emitReadable_ (node:internal/streams/readable:573:12)
    at onEofChunk (node:internal/streams/readable:551:5)
    at readableAddChunk (node:internal/streams/readable:267:5) {
  code: 'TypeError',
  time: 2021-09-29T05:46:30.754Z,
  statusCode: 200,
  retryable: false,
  retryDelay: 61.83578978099382
}
根因说明

AWS SDK v2 的DynamoDB操作方法仅支持回调模式和Promise模式二选一,不能同时传入回调函数又调用.promise()方法。两种模式混用会导致SDK内部处理响应的上下文属性未正确初始化,进而触发访问undefined属性的报错。

修复方案

删除dynamodb.scan()方法中传入的回调函数,所有逻辑统一用try/catch处理即可:

async function getVibefinderPhotos() {
  const params = {
    TableName: "vibefinder-photos",
  };
  try {
    const r = await dynamodb.scan(params).promise();
    console.log(r)
    return r;
  } catch (e) {
    console.log("Error scan ", e);
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 11:54:03