无服务器错误类型错误:无法读取未定义的属性“toLowerCase”

Posted

技术标签:

【中文标题】无服务器错误类型错误:无法读取未定义的属性“toLowerCase”【英文标题】:Serverless Error TypeError: Cannot read property 'toLowerCase' of undefined 【发布时间】:2021-11-08 22:37:08 【问题描述】:

我尝试从serverless 部署类似this example 的东西。构建我的serverless.yml,我遇到了这个错误,我找不到处理的句柄:

service: products-api

package:
  artifact: target\products-api-dev.jar
  #artifact: target\$self:service-$self:provider.stage.jar #cool alternative :)

provider:
  name: aws
  runtime: java8

#Copy-pasted
resources:
  Resources:
    productsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: products_table
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: name
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
          - AttributeName: name
            KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1

functions:
  listProducts:
    handler: com.serverless.ListProductsHandler
    events:
      - http:
          path: /products
          method: get
  getProduct:
    handler: com.serverless.GetProductHandler
    events:
      - http:
          path: /products/id
          method: get
  createProduct:
    handler: com.serverless.CreateProductHandler
    events:
      - http:
          path: /products
          method: post
  deleteProduct:
    handler: com.serverless.DeleteProductHandler
    events:
      - http:
          path: /products/id

我得到的错误是——在我自己的代码中没有对错误的引用很难发现我出错的地方。 我查看了许多 Q/A,包括 this 和 this,但那里似乎更多的是 javascript 和打字稿问题,而不是这里的无服务器。

 Type Error ----------------------------------------------

  TypeError: Cannot read property 'toLowerCase' of undefined
      at AwsCompileApigEvents.getHttpMethod (C:\snapshot\serverless\lib\plugins\aws\package\compile\events\apiGateway\lib\validate.js:195:24)
      at C:\snapshot\serverless\lib\plugins\aws\package\compile\events\apiGateway\lib\validate.js:50:30
      at Array.forEach (<anonymous>)
      at C:\snapshot\serverless\lib\plugins\aws\package\compile\events\apiGateway\lib\validate.js:45:37
      at Array.forEach (<anonymous>)
      at AwsCompileApigEvents.validate (C:\snapshot\serverless\lib\plugins\aws\package\compile\events\apiGateway\lib\validate.js:44:55)
      at Object.package:compileEvents [as hook] (C:\snapshot\serverless\lib\plugins\aws\package\compile\events\apiGateway\index.js:318:31)
      at PluginManager.invoke (C:\snapshot\serverless\lib\classes\PluginManager.js:579:20)
      at async PluginManager.spawn (C:\snapshot\serverless\lib\classes\PluginManager.js:601:5)
      at async Object.before:deploy:deploy [as hook] (C:\snapshot\serverless\lib\plugins\deploy.js:60:11)
      at async PluginManager.invoke (C:\snapshot\serverless\lib\classes\PluginManager.js:579:9)
      at async PluginManager.run (C:\snapshot\serverless\lib\classes\PluginManager.js:639:7)
      at async Serverless.run (C:\snapshot\serverless\lib\Serverless.js:452:5)
      at async C:\snapshot\serverless\scripts\serverless.js:751:9
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

【问题讨论】:

【参考方案1】:

andymac4182 有the solution for your problem 的想法,实际上很简单。您必须为每个 http 元素定义一个方法,而对于最后一个,这是缺失的:method: post

更新:

  deleteProduct:
    handler: com.serverless.DeleteProductHandler
    events:
      - http:
          path: /products/id
          method: post

【讨论】:

【参考方案2】:

Serverless 是一个 javascript 项目,这就是您看到 javascript 错误的原因。

您的功能之一似乎是 HTTP 端点,但缺少任何方法。这似乎是错误的原因。

尝试将method: DELETE 添加到您的端点:

  deleteProduct:
    handler: com.serverless.DeleteProductHandler
    events:
      - http:
          path: /products/id
          method: DELETE

【讨论】:

以上是关于无服务器错误类型错误:无法读取未定义的属性“toLowerCase”的主要内容,如果未能解决你的问题,请参考以下文章

未捕获的类型错误:无法读取未定义的属性(读取“替换”)

未捕获的类型错误:无法读取未定义的属性“获取”

无法读取未定义类型错误的属性“推送”:无法读取未定义错误的属性“推送”

jstree:未捕获的类型错误:无法读取未定义的属性“孩子”

未捕获的类型错误:无法读取未定义的属性“释放”

jsonp 请求中的“未捕获的类型错误:无法读取未定义的属性‘toLowerCase’”