使用 LB4 框架和 Postgres for DB,使用 "loopback-connector-postgresql": "^3.7.0" 进行数据库连接,并在使用 find 查询和 where 运算符来获取数据列表时遇到问题下面分享了数组中的匹配值和所使用的查询,
样本表数据:
[{id: 1,
roll: [1,4]
},{
id:2,
roll: [1,3],
},{
id:3,
roll: [2,4]
}]
在模型卷中标记为必填
使用的查询:
this.repository.find({where: { roll: { inq: [4] }}});
预期输出:
[{id: 1,
roll: [1,4]
},{
id:3,
roll: [2,4]
}]
对于上面的代码片段,loopback4 中的打字稿会抛出以下错误
*Argument of type '{ where: { roll: { inq: number[]; }; }; }' is not assignable to parameter of type 'Filter<ModelRow>'.
Types of property 'where' are incompatible.
Type '{ row: { inq: number[]; }; }' is not assignable to type 'Condition<ModelRow> | AndClause<ModelRow> | OrClause<ModelRow> | undefined'.
Type '{ row: { inq: number[]; }; }' is not assignable to type 'Condition<ModelRow>'.
Types of property 'row' are incompatible.
Type '{ inq: number[]; }' is not assignable to type 'PredicateComparison<number[]> | (number[] & string) | (number[] & number) | (number[] & false) | (number[] & true) | (number[] & Date) | undefined'.
Type '{ inq: number[]; }' is not assignable to type 'undefined'.ts(2345)*
我遇到了同样的问题,但是是字符串。通过强制转换到任何自定义包装器来破解它。也许也可以通过这种方式破解号码。脏溶液
where: {rolls: {inq: [`"{${rollValue}}"`] as any}},
如果您使用 Postgresql,请使用
ilike
过滤器
where: {rolls: {ilike: `%${rollValue}`}}
inq
是一个过滤运算符,常用于 MongoDB 等非关系数据库。
ilike
是一个过滤运算符,通常用于 PostgreSQL 等关系数据库