无服务器:此服务中不存在功能

时间:2018-06-24 21:41:21

标签: javascript serverless-framework serverless aws-serverless

在无服务器环境中,我的功能具有以下目录结构:

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 part,但是无论我如何更改处理程序,这些函数似乎都不存在。

我尝试将路径更改为functions/stories/create/create.main没什么区别,是否有明显的缺失,我不允许在同一位置使用多个处理程序?

我正在看下面的example,它使用了一个文件夹“ todos”,其中包含多个功能,但是我看不到它和我的文件夹之间有任何明显的区别,只是我添加了一个额外的文件夹

1 个答案:

答案 0 :(得分:4)

您的模板无效。您不能只是将函数放在任意节点下以告诉框架它适用于您的应用程序的某些对象。您的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