首页 > 解决方案 > loopback 4如何使用有一个get方法

问题描述

我已经建立了两个模型产品和产品价格之间的一种关系。产品有一个产品价格,产品价格属于产品。但是我无法使用在成功实现有一个关系后添加到产品存储库的 get 方法。

这是代码

@get('/products/{id}/product-price', {
    responses: {
      '200': {
        description: 'Array of productPrice\'s belonging to Product',
        content: {
          'application/json': {
            schema: { type: 'array', items: getModelSchemaRef(ProductPrice) },
          },
        },
      },
    },
  })
  async find(
    @param.path.number('id') id: number,

    @param.query.object('filter') filter?: Filter<ProductPrice>,
    @param.query.object('where', getWhereSchemaFor(ProductPrice)) where?: Where<ProductPrice>,

  ): Promise<ProductPrice[]> {
    return this.productRepository.productPrices(id).get(filter) // error
  }

这是错误

Type 'ProductPrice' is missing the following properties from type 'ProductPrice[]': length, pop, push, concat, and 26 more

标签: loopbackjsloopback4

解决方案


我认为你应该回到 TypeScript 问题。您正在查询 beLongTo 这意味着您的回答应该是Promise<ProductPrice>而不是Promise< ̶P̶r̶o̶d̶u̶c̶t̶P̶r̶i̶c̶e̶[̶]̶>


推荐阅读