当查询为空时,是否有一些方法可以使用默认值?
如果我有以下用于查询的DTO:
export class MyQuery {
readonly myQueryItem: string;
}
如果我的请求中不包含查询,那么myQuery.myQueryItem
将是未定义的。我该如何使其具有默认值?
当查询为空时,是否有一些方法可以使用默认值?
如果我有以下用于查询的DTO:
export class MyQuery {
readonly myQueryItem: string;
}
如果我的请求中不包含查询,那么myQuery.myQueryItem
将是未定义的。我该如何使其具有默认值?
您可以直接在DTO类中设置默认值:
export class MyQuery {
readonly myQueryItem = 'mydefault';
}
你需要实例化该类,以便使用默认值。为此,您可以例如使用带有选项transform: true
的ValidationPipe
。如果该值由您的查询参数设置,则它将被覆盖。
@Get()
@UsePipes(new ValidationPipe({ transform: true }))
getHello(@Query() query: MyQuery) {
return query;
}
1) 管道可以应用于所有的装饰器,例如 @Body()
, @Param()
, @Query()
并且可以转换值(例如 ParseIntPipe
)或执行检查(例如 ValidationPipe
)。
2) ValidationPipe
内部使用 class-validator
和 class-transformer
进行验证。为了能够对您的输入(普通 JavaScript 对象)执行验证,它首先必须将它们转换为您的注释 dto 类的实例,这意味着它创建了您类的一个实例。使用设置 transform: true
将自动创建您的 dto 类的实例。
示例(基本操作方式):
class Person {
firstname: string;
lastname?: string = 'May';
constructor(person) {
Object.assign(this, person);
}
}
// You can use Person as a type for a plain object -> no default value
const personInput: Person = { firstname: 'Yuna' };
// When an actual instance of the class is created, it uses the default value
const personInstance: Person = new Person(personInput);
在您的 Dto 中只需像这样提供一个值:
export class MyQuery {
readonly myQueryItem: string = 'value default';
}
myQueryItem
仍然未定义。 - papillon
npm ci
命令,但是没有任何变化。 - papillon@Query(new ValidationPipe())
),对吗?transform:true
具体是什么意思?为什么需要使用DTO的默认值,否则就不会使用这些默认值? 请注意,这是计算机翻译的结果,可能需要人工校对。 - papillonValidationPipe
的选项中设置transformOptions: { exposeDefaultValues: true }
。 - EzPizzatransform
设置为true
,就可以按照描述的那样正常工作。 - vinnymac