【问题标题】:LOOPBACK 4: Add parameters in a API callLOOPBACK 4:在 API 调用中添加参数
【发布时间】:2019-11-17 03:17:16
【问题描述】:

我是 Loopback 4 (NodeJS) 的新手,我有一个问题。我正在开发一个 API。如何在 post 请求的正文中指示未定义为模型的参数?。

例子:

@post('/gameshits/{id}', {
    responses: {
      '200': {
        description: 'Return the number of correct answers',
      },
    },
  })
  async gamesHits(
    @param.path.string('id') id: string,
    @requestBody() answers: Array<String>,
  ): Promise<number> {
     ....
}

问题出在 requestBody() 它编译但在环回/资源管理器中说它可以被渲染。唯一的选择是创建一个模型?如何在调用正文中添加更多参数以发送? (不像@param那样在url中)

谢谢。

【问题讨论】:

    标签: node.js typescript loopback


    【解决方案1】:

    不,您不需要创建模型来指示参数,实际上这很容易,但没有太多关于它的文档。

    你可以这样表示。

    @post('/gameshits/{id}', {
      responses: {
        '200': {
          description: 'Return the number of correct answers',
        },
      },
    })
    async gamesHits(
      @param.path.string('id') id: string,
      @requestBody({
        content: {
          'application/json': {
            type: 'object',
            schema: {
              properties: {
                gameName: {type: 'string'},
                characters: {
                  type: 'array',
                  items: {
                    properties: {
                      name: {type: 'number'},
                      power: {type: 'number'},
                      cost: {type: 'number'},
                      ability: {type: 'string'}
                    },    
                  },
                },
              },
            },
          },
        },
      }) data: any,
    ): Promise<number> {
       ....
    }
    

    获得这样的环回/探索器响应。

    {
      "gameName": "string",
      "characters": [
        {
          "name": 0,
          "power": 0,
          "cost": 0,
          "ability": "string"
        }
      ]
    }
    

    【讨论】:

      【解决方案2】:

      您不需要任何文档,只需要 swagger 关于描述请求的文档:https://swagger.io/docs/specification/2-0/describing-request-body/?sbsearch=request

      然后只需在 @requestBody 装饰器中应用 OpenAPI 规范,如 darkunbeknownst 所述。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-01
        • 1970-01-01
        • 2020-01-23
        • 2014-12-16
        • 2023-04-03
        • 1970-01-01
        • 2021-01-10
        相关资源
        最近更新 更多