gpt4 book ai didi

typescript - 使用 TypeScript 在 mongoose.Schema 中的 `required` 字段中使用函数

转载 作者:行者123 更新时间:2023-12-04 13:38:46 24 4
gpt4 key购买 nike

我正在尝试创建一个动态模式,其中需要基于另一个字段的值的字段。

示例架构:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com