首页 > 解决方案 > 通过 Express / 节点内的 AWS SDK 扫描 DDB 时的 Request.HTTP_DATA

问题描述

我正在设置一个访问 AWS DynamoDB 的 Node Express API,但我遇到了一些问题。

我仍在学习如何最好地处理异步/等待,但我无法弄清楚为什么下面的代码:

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
}

标签: node.jsexpressasync-awaitaws-sdk-js

解决方案


推荐阅读