【问题标题】:How to apply search api in loopback4如何在 loopback4 中应用搜索 api
【发布时间】:2019-12-21 07:47:00
【问题描述】:

我正在通过 get api 获取产品列表。我想在该 get api 中应用搜索 api(我们正在获取产品的完整列表)。那就是我想将 name 参数作为可选参数。如果用户在搜索框中输入名称,那么 api 将按名称搜索该产品并显示它。如果用户不输入名称,那么它将显示产品的完整列表。这是我的 loopback4 代码

      @get('/zones', {
responses: {
  '200': {
    description: 'Array of Zone model instances',
    content: {
      'application/json': {
        schema: {
          type: 'array',
          items: getModelSchemaRef(Zone, {includeRelations: true}),
        },
      },
    },
  },
},
 })
  async find(
   //@param.path.string('name') name: String,
   @param.query.object('filter', getFilterSchemaFor(Zone)) filter?: 
Filter<Zone>,
 ): Promise<Zone[]> {
 return this.zoneRepository.find({include: [{relation: 'cameras'}]});
  }

【问题讨论】:

    标签: loopback4


    【解决方案1】:

    拥有不同的搜索 API 总是好的。

    您可以使用 where 过滤器进行搜索,如下所示

    filter[where][property][regexp]=searchstring
    

    无论如何,如果您仍然想按照自己的方式进行操作。检查参数是否存在。如果是,则使用上述过滤器,否则返回所有数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-12
      • 2015-03-20
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多