我正在使用loopback4
我已经在模型上正确定义了以下关系。
Owner hasMany Transaction
Transaction belongsTo owner
Transaction belongsTo Bank
我想找到所有属于一个所有者的交易,并将银行详细信息包括在响应对象中。这就是我要做的
// OwnerRepository
// owner.repository.ts
const transList = this.transactionRepository.find({ include: [Bank] })
但是我不知道include过滤器是如何工作的。一切关系都很好。我正在使用mongodb
作为数据库。
请向我说明如何正确使用包含过滤器。
谢谢
答案 0 :(得分: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;
}
答案 1 :(得分:0)
您可以覆盖过滤器对象的where方法属性,并使用Content-Type: application/json
参数进行过滤。
基本上是这样的:
[HttpGet]
[Route("/api/v{version:apiVersion}/[controller]/{id}.json")]
public IActionResult PersonAsJson([FromRoute] int id, [FromQuery] bool pretty = false)
{
var person = new Person(id)
// ...
Response.Headers.Add("Content-Type", "application/json");
if (pretty)
{
return Ok(JsonSerializer.Serialize(
person,
new JsonSerializerOptions { WriteIndented = true }));
}
// non pretty output if there's no
// services.AddControllers().AddJsonOptions(
// options => options.JsonSerializerOptions.WriteIndented = true);
// in Startup.cs
return Ok(person);
}
基本上将根据您的要求覆盖where属性。并且在这种情况下,如果您还通过了其他一些过滤器(例如限制),则它们仍将存在于过滤器中。
希望这会有所帮助。