首页 > 解决方案 > 无服务器离线:“路径”参数必须是字符串类型。收到未定义

问题描述

不知道为什么会发生这种情况,但我有一个非常简单的无服务器应用程序正在运行,但现在当我运行时,sls offline start我得到了上面的错误。我找到了罪魁祸首,它是events函数内部。

这是serverless.yml文件:

service: hello-world-offline

provider:
  name: aws
  runtime: nodejs12.x
  region: eu-east-1
  stage: dev

plugins:
  - serverless-offline

functions:
  hello-world:
    handler: handler.handle # required, handler set in AWS Lambda
    events:
      - http:
          path: hello-world
          method: get
          cors: true

这是handler.js文件:

module.exports.handle = async (event, ctx, cb) => {
  cb(null, {
    statusCode: 200,
    body: JSON.stringify({ message: "hello world" })
  })
}

如果我去掉events函数中的hello-world一切都可以正常工作,sls offline start除了我当然不能在本地实际命中端点。我尝试通过添加引号使其成为硬字符串,但没有任何作用。

编辑:原来这发生在使用yarn workspaces. 如果我把它放在一个packages/my-serverless-app结构中并 cd 到文件夹中运行sls offline start命令,就会发生这种情况。如果我将它从结构中移除,它就可以正常工作。

标签: yarnpkgserverlessyarn-workspacesserverless-offline

解决方案


改变

events:
      - http:
          path: hello-world
          method: get
          cors: true

events:
      - httpApi:
          path: hello-world
          method: get

这应该工作!


推荐阅读