Bor*_*ter 2 parameters query-string routeparams nestjs
我设置了这条路线:
@Get('/name/like')
findByLikeName(@Query() query: { supplierID: string; name: string }) {
return this.supplierProductService.findByLikeName(query);
}
Run Code Online (Sandbox Code Playgroud)
它利用底层服务中的查询参数:
async findByLikeName({
supplierID,
name,
}: {
supplierID: string;
name: string;
}): Promise<SupplierProduct[]> {
return await this.supplierProductRepository.findAll({
where: {
name: {
[Op.like]: `%${name}%`,
},
supplierID: supplierID,
},
});
}
Run Code Online (Sandbox Code Playgroud)
但是,假设我想将 sellerID 移动到 /:supplierID 路由参数中,同时在查询对象中维护名称(以及潜在的其他查询参数),我将如何实现这一点?
你几乎已经明白了。您需要做的就是设置路由@Get()以了解它正在使用 URL 参数并执行以下操作:
@Get('/name/like/:supplierID')
findByLikeName(
@Query() query: { name: string },
@Param() param: { supplierID: string }
) {
return this.supplierProductService.findByLikeName({...query, ...param});
}
Run Code Online (Sandbox Code Playgroud)