【发布时间】:2021-05-22 10:12:02
【问题描述】:
我目前正在学习 TS,并且我正在重新编写我的旧 NodeJS/Express 项目。我需要在验证器中引用另一个文档的字段,但我收到一个错误:
类型 '{ 验证器: (val: number) => boolean; 上不存在属性 'price' }'.ts(2339).
代码如下:
const tourSchema = new mongoose.Schema({
price: {
type: Number,
required: [true, ' A tour must have a price']
},
priceDiscount: {
validator: function(val: number): boolean {
return val < this.price
},
message: 'Discount price ({VALUE}) should be below regular price'
}
});
如您所见,我需要 priceDiscount 验证器,但我无法引用文档中的其他字段,因为 TS 不知道该字段是否存在。我怎样才能知道我想引用同一文档中的另一个字段?
【问题讨论】:
标签: javascript node.js typescript mongoose mongoose-schema