Node.js Amazon DynamoDB扫描向服务器发出两个HTTP请求,引发错误,为什么?

Node.js Amazon DynamoDB扫描向服务器发出两个HTTP请求,引发错误,为什么?,node.js,typescript,amazon-web-services,http,amazon-dynamodb,Node.js,Typescript,Amazon Web Services,Http,Amazon Dynamodb,我有一个typescriptreact应用程序,它使用DynamoDB服务存储数据。 我使用了javascript中间件来管理数据库访问操作。 我在本地开发了扫描、放置和删除操作,没有错误。 一旦设置了生产环境,就会出现错误。 如果使用生产环境在本地执行代码,则会出现相同的错误 我注意到扫描操作失败了。 实际上,它以一种奇怪的方式失败了,因为我看到对aws服务器的请求被执行了两次 为了简化调试过程,我在react-onClick方法中调试了所有db访问,以便将操作与应用程序的生命周期隔离开来 我

我有一个typescriptreact应用程序,它使用DynamoDB服务存储数据。 我使用了javascript中间件来管理数据库访问操作。 我在本地开发了扫描、放置和删除操作,没有错误。 一旦设置了生产环境,就会出现错误。 如果使用生产环境在本地执行代码,则会出现相同的错误

我注意到扫描操作失败了。 实际上,它以一种奇怪的方式失败了,因为我看到对aws服务器的请求被执行了两次

为了简化调试过程,我在react-onClick方法中调试了所有db访问,以便将操作与应用程序的生命周期隔离开来

我注意到

  • onClick fetch操作实际上只调用了一次(如预期的那样)
  • DynamoDB扫描操作确实调用了一次(如预期的那样)
  • scann的Callback执行两次(不是预期的)
    • 第一次确实获取数据,我可以记录它(完美)
    • 执行第二个调用时出错
aws配置代码

const awsConfig = {
  accessKeyId: process.env.REACT_APP_AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.REACT_APP_AWS_SECRET_ACCESS_KEY,
  region: process.env.REACT_APP_AWS_REGION,
  endpoint: process.env.REACT_APP_AWS_ENDPOINT
}

new AWS.Endpoint(awsConfig.endpoint || '')

AWS.config.update({
  region: awsConfig.region,
  //endpoint: awsConfig.endpoint || '',
  credentials: new AWS.Credentials(awsConfig.accessKeyId || '', awsConfig.secretAccessKey || ''),
  sslEnabled: false,
  maxRetries: 5
})

const dynamoDbClient = new AWS.DynamoDB.DocumentClient()
const searchParams = { TableName: 'table' }

let dataItems: DateFields[] = []

await dynamoDbClient
  .scan(searchParams, (error: any, data: any) => {
    if (error) {
      console.error(`Error fetchCategories: `, JSON.stringify(error, null, 2))
      return
    }
    dataItems = data.Items
  })
  .promise()
请注意这部分代码,如果我将端点保留在AWS.config.update中,那么Typescript会给我一个错误,因此我将其设置为不同的new AWS.endpoint(awsConfig.endpoint | |“”)

aws访问代码

const awsConfig = {
  accessKeyId: process.env.REACT_APP_AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.REACT_APP_AWS_SECRET_ACCESS_KEY,
  region: process.env.REACT_APP_AWS_REGION,
  endpoint: process.env.REACT_APP_AWS_ENDPOINT
}

new AWS.Endpoint(awsConfig.endpoint || '')

AWS.config.update({
  region: awsConfig.region,
  //endpoint: awsConfig.endpoint || '',
  credentials: new AWS.Credentials(awsConfig.accessKeyId || '', awsConfig.secretAccessKey || ''),
  sslEnabled: false,
  maxRetries: 5
})

const dynamoDbClient = new AWS.DynamoDB.DocumentClient()
const searchParams = { TableName: 'table' }

let dataItems: DateFields[] = []

await dynamoDbClient
  .scan(searchParams, (error: any, data: any) => {
    if (error) {
      console.error(`Error fetchCategories: `, JSON.stringify(error, null, 2))
      return
    }
    dataItems = data.Items
  })
  .promise()
有以下错误

Error fetchCategories:  {
  "message": "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.",
  "code": "InvalidSignatureException",
  "time": "2020-04-29T08:39:14.449Z",
  "requestId": "xxxxxxxxxxxxxxxxxxx",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 15.082420221534553
}

{"__type":"com.amazon.coral.service#InvalidSignatureException","message":"The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details."}

知道为什么会这样吗?非常感谢

问题在于aws访问代码,为了解决它,我更新了DynamoDB客户端,如下所示:

  try {
    const data = await dynamoDbClient.scan(params).promise()
    return data.Items
  } catch(error) {
    logError(operation, error)
  }
代码中的此更改可防止:

  • 要执行两次的回调
  • 服务器回答的错误可能反映了AWS安全机制阻止了多个相同和相应的服务器查询

问题在于aws访问代码中,为了解决这个问题,我更新了DynamoDB客户端,如下所示:

  try {
    const data = await dynamoDbClient.scan(params).promise()
    return data.Items
  } catch(error) {
    logError(operation, error)
  }
代码中的此更改可防止:

  • 要执行两次的回调
  • 服务器回答的错误可能反映了AWS安全机制阻止了多个相同和相应的服务器查询