小编LaC*_*deM的帖子

如何在 Nest.js 中获取嵌套 URL 查询

我有 NestJS 应用程序,我必须从 URL 获取查询。问题是我的creationDate是一个对象,我无法通过@Query将其作为嵌套对象获取。

以下是路线示例:

xxx/yyy/orders?key1=value&key2=value&key3=value&createdAt[lte]=2021-07-01&createdAt[gte]=2011-03-30&orderBy=desc&limit=500
Run Code Online (Sandbox Code Playgroud)

我正在尝试将createdAt[lte]和createdAt[gte]作为嵌套对象

export interface QueryI {
key1: string;
key2: string;
key3: string;
key4: string;
createdAt: {
    lte: string,
    gte: string,
}
orderBy: 'desc' | 'asc';
limit: number;
}
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

@Get('route')
getAll(
    @Query() query: QueryI): Promise<void> { 
    return this.myService.findAll(query);
}
Run Code Online (Sandbox Code Playgroud)

但这给了我以下结果

{

key1: 'value',        
  key2: 'value',
  key3: 'value',       
  key4: 'value',
  'createdAt[lte]': 'some date',
  'createdAt[gte]': 'some date',
  orderBy: 'desc',
  limit: '500'

}
Run Code Online (Sandbox Code Playgroud)

我尝试过 JSON.stringify 并在 SOW 上咨询了类似的问题,但没有运气。

谢谢

javascript node.js typescript routeparams nestjs

8
推荐指数
1
解决办法
8603
查看次数

标签 统计

javascript ×1

nestjs ×1

node.js ×1

routeparams ×1

typescript ×1