【问题标题】:Unable to determine event type for AWS Lambda serverless function无法确定 AWS Lambda 无服务器函数的事件类型
【发布时间】:2021-02-09 00:53:55
【问题描述】:

我有一个非常简单的处理程序,我用它来熟悉无服务器 lambda 函数。

const example: APIGatewayProxyHandler = async event => {
  console.log(JSON.stringify(event, null, 2));
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Success',
    }),
  };
};

这是我的 serverless.yml 文件:

service: lambda-example
frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x

plugins:
  - serverless-offline

functions:
  example:
    handler: lib/handler.example
    events:
      - http:
          path: example
          method: get
          integration: lambda

当我点击 API 时,它工作得很好......

{
  statusCode: 200,
  body: "{"message":"Success"}"
}

我正在使用此 URL 来触发事件 http://localhost:3000/dev/example?url=https://google.com/,并且当端点被命中时,我正在控制台记录事件本身...

{
  "body": {},
  "method": "GET",
  "principalId": "offlineContext_authorizer_principalId",
  "stage": "dev",
  "cognitoPoolClaims": {
    "sub": ""
  },
  "enhancedAuthContext": {
    "principalId": "offlineContext_authorizer_principalId"
  },
  "headers": {
    "Host": "localhost:3000",
    "Connection": "keep-alive",
    "Cache-Control": "max-age=0",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-User": "?1",
    "Sec-Fetch-Dest": "document",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.9,la;q=0.8"
  },
  "query": {
    "url": "https://google.com/"
  },
  "path": {},
  "identity": {
    "accountId": "offlineContext_accountId",
    "apiKey": "offlineContext_apiKey",
    "apiKeyId": "offlineContext_apiKeyId",
    "caller": "offlineContext_caller",
    "cognitoAuthenticationProvider": "offlineContext_cognitoAuthenticationProvider",
    "cognitoAuthenticationType": "offlineContext_cognitoAuthenticationType",
    "sourceIp": "127.0.0.1",
    "user": "offlineContext_user",
    "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36",
    "userArn": "offlineContext_userArn"
  },
  "stageVariables": {},
  "requestPath": "/example"
}

我有兴趣访问该对象的“查询”属性,但是当我尝试时 TypeScript 对我很生气......

console.log(event.query);

===

Property 'query' does not exist on type 'APIGatewayProxyEvent'.

查询属性非常清楚地存在于事件对象上,如我的控制台日志中所示,因此我假设我错误地键入了此函数,但我似乎找不到正确的类型。我在这里做错了什么?

附加信息(package.json):

"dependencies": {
  "axios": "^0.21.0",
  "cheerio": "^1.0.0-rc.3"
},
"devDependencies": {
  "@types/aws-lambda": "^8.10.64",
  "@types/cheerio": "^0.22.22",
  "@types/node": "^14.14.2",
  "@typescript-eslint/eslint-plugin": "^4.5.0",
  "@typescript-eslint/parser": "^4.5.0",
  "eslint": "^7.12.0",
  "eslint-config-prettier": "^6.14.0",
  "husky": "^4.3.0",
  "prettier": "^2.1.2",
  "prettier-plugin-organize-imports": "^1.1.1",
  "serverless-dotenv-plugin": "^3.1.0",
  "serverless-offline": "^6.8.0",
  "typescript": "^4.0.3"
}

【问题讨论】:

    标签: aws-lambda serverless-framework serverless aws-serverless


    【解决方案1】:

    原来问题出在

    integration: lambda
    

    添加到我的 serverless.yml 文件。当我删除这一行时,APIGatewayProxyHandler 接口按预期工作。

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 2019-02-24
      • 2019-07-23
      • 2017-10-01
      • 2018-06-25
      • 2023-03-16
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多