2

在无服务器中,我的函数具有以下目录结构:

serverless.yml
functions -
    stories -
        create.js
        get.js

我的serverless.yml然后看起来像这样:

functions:
  stories:
    create:
      handler: functions/stories/create.main
      events:
        - http:
          path: stories/create
          method: post
          cors: true
          authorizer: aws_iam
    get:
      handler: functions/stories/get.main
      events:
        - http:
          path: stories/{id}
          method: get
          cors: true
          authorizer: aws_iam

但是,当我运行测试以检查创建时:serverless invoke local --function create --path mocks/create-event.json我收到以下错误:

Serverless Error ---------------------------------------

  Function "create" doesn't exist in this Service

我设法让一个看起来像这样的函数工作:

functions:
  stories:
    handler: functions/stories/create.main
    events:
      - http:
        path: stories/create
        method: post
        cors: true
        authorizer: aws_iam

由于我添加了 get 函数,我决定需要在故事之后添加 create 和 get 部分,但无论我如何更改处理程序,这些函数似乎都不存在。

我尝试将路径更改为functions/stories/create/create.main没有区别,是否有任何明显的我遗漏以允许同一位置内的多个处理程序?

我在看下面的例子,它使用一个包含多个功能的“todos”文件夹,但我看不出它和我的有任何明显区别,除了我添加了一个额外的文件夹。

4

2 回答 2

9

您的模板无效。您不能只是将您的函数放在任意节点下以告诉框架它适用于您的应用程序的某些对象。您的stories:节点应该是评论。

尝试这样的事情:

functions:
    # Stories related functions
    createStory:
      handler: functions/stories/create.main
      events:
        - http:
            path: stories   # You can post directly to stories to be more RESTful
            method: post
            cors: true
            authorizer: aws_iam
    getStory:
      handler: functions/stories/get.main
      events:
        - http:
            path: stories/{id}
            method: get
            cors: true
            authorizer: aws_iam

     # More examples to understand the routing
    getAllStories:
      handler: functions/stories/getAll.main # Returns a list of all stories
      events:
        - http:
            path: stories
            method: get
            cors: true
            authorizer: aws_iam
    deleteStory:
      handler: functions/stories/delete.main # Deletes a story
      events:
        - http:
            path: stories/{id}
            method: delete
            cors: true
            authorizer: aws_iam
于 2018-06-29T14:54:31.493 回答
1

花了这么多时间!刚刚发现当我输入命令时我提到了handle.js文件中的函数名例如这是错误的:

在此处输入图像描述

sls invoke local --function testFunction

这是正确的:

在此处输入图像描述

sls invoke local --function test
于 2022-01-09T14:56:18.780 回答