Javascript 使用typescript中的类验证器创建自定义函数和默认属性
如何在类验证器DTO中创建自定义函数,以及如何在从DB检索对象后调用它们。另外,如何在插入数据时应保存的同一类中声明默认属性 我有一个带有类验证器的DTO类:Javascript 使用typescript中的类验证器创建自定义函数和默认属性,javascript,node.js,typescript,class-validator,sequelize-typescript,Javascript,Node.js,Typescript,Class Validator,Sequelize Typescript,如何在类验证器DTO中创建自定义函数,以及如何在从DB检索对象后调用它们。另外,如何在插入数据时应保存的同一类中声明默认属性 我有一个带有类验证器的DTO类: export class CustomDTOClass { @IsString() propOne: string | null; @IsString() propTwo: string | null; <how can I make this default with value tr
export class CustomDTOClass {
@IsString()
propOne: string | null;
@IsString()
propTwo: string | null;
<how can I make this default with value true for eg. with any decorator?>
readonly someDefaultProp: boolean = true;
<this function doesn't print when an object of CustomDTOClass is retrived from DB>
public print(name: string) {
console.log('HELLO :' + name);
console.log('YOUR PROPS ONE:' + this.propOne);
console.log('YOUR PROPS TWO:' + this.propTwo);
}
constructor(propOne: string, propTwo: string) {
this.propOne = propOne;
this.propTwo = propTwo;
}
}
在重试时,调用print()
无效
const customDTO = await CustomModel.findByPk(1);
customDTO.print('JOE');
它抛出:
TypeError: customDTO.print is not a function
我做错了什么?如何在类验证器中创建带有注释的默认值?能否粘贴CustomModel的外观?
TypeError: customDTO.print is not a function