我正在尝试创建一个动态模式,其中需要基于另一个字段的值的字段。
示例架构:
const foo = new Schema({
status: {
type: String,
default: "in_process"
},
route: {
type: String,
default: ""
},
code: {
type: Number,
default: 0,
required: function () {
return this.route === "Results";
}
},
});
当我这样做时,TS 通知我这个错误:
Property 'route' does not exist on type 'Schema<any> | SchemaTypeOpts<any> | SchemaType'.
Property 'route' does not exist on type 'Schema<any>'.
我应该如何正确处理这种情况?
最佳答案
interface Ifoo extends Document {
status:string
route:string
code:number
}
const foo = new Schema({
status: {
type: String,
default: "in_process"
},
route: {
type: String,
default: ""
},
code: {
type: Number,
default: 0,
required: function (this:Ifoo) {
return this.route === "Results";
}
},
});
关于typescript - 使用 TypeScript 在 mongoose.Schema 中的 `required` 字段中使用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60209292/