0

我正在开发一个帐户管理系统,使用loopback4

我已经在我的模型上正确定义了以下关系。

Owner hasMany Transaction
Transaction belongsTo owner

Transaction belongsTo Bank

我想找到所有交易属于一个所有者,并在响应对象中包含银行详细信息。这就是我尝试做的

// OwnerRepository
// owner.repository.ts

const transList = this.transactionRepository.find({ include: [Bank] })

但我无法弄清楚包含过滤器是如何工作的。所有关系都运行良好。我正在使用mongodb数据库。

请解释我如何正确使用包含过滤器。

谢谢

4

2 回答 2

0

您可以覆盖过滤器对象的 where 方法属性,并使用inq参数进行过滤。

基本上是这样的:

filter.where = {id: { inq: objectAsPerId }};
and then
const transList = this.transactionRepository.find(filter);

这基本上会根据您的要求覆盖 where 属性。在这种情况下,如果您还通过了其他一些过滤器,例如限制,偏移它们仍然存在于过滤器中。

希望这会有所帮助。

于 2019-10-18T11:06:38.503 回答
0

它似乎还没有实现https://github.com/strongloop/loopback-next/issues/1889 https://github.com/strongloop/loopback-next/issues/1352

您必须自己进行过滤:示例:

async find( @param.query.object('filter', getFilterSchemaFor(Cuenta)) filter?: Filter<Cuenta>): Promise<any[]> {

     let cuenta = await this.cuentaRepository.find(filter);
     console.log(cuenta);
     return this.populateDuenio(cuenta);
}

async populateDuenio(cuentas: Cuenta[]) :Promise<any[]>{
    let cuentasPopulate = [];
    for (let i:number = 0; i < cuentas.length; i++) {
       let persona = await this.cuentaRepository.persona(cuentas[i].id);
       cuentasPopulate.push( {
          ...cuentas[i],
          personaId:{
          ...persona
        }
     })
 }
   return cuentasPopulate;
}
于 2019-10-01T02:51:15.763 回答