[英]Typescript error when working with mongoose ( running inside kubernetes cluster and skaffold )
The code is,代码是,
const userSchema = new mongoose.Schema({
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
});
console.log(userSchema);
userSchema.statics.build = (user: UserAttrs) => {
return new User(user);
};
userSchema.pre("save", async function (next) {
if (this.isModified("password")) {
const hashed = await Password.toHash(this.get("password"));
this.set("password", hashed);
}
next();
});
Now, the error I'm running into is,现在,我遇到的错误是,
[auth] > auth@1.0.0 start /app
[auth] > ts-node-dev src/index.ts
[auth]
[auth] [INFO] 12:46:59 ts-node-dev ver. 1.0.0 (using ts-node ver. 9.0.0, typescript ver. 3.9.7)
[auth] Compilation error in /app/src/models/user.ts
[auth] [ERROR] 12:47:04 ⨯ Unable to compile TypeScript:
[auth] src/models/user.ts(37,12): error TS2551: Property 'statics' does not exist on type 'Schema'. Did you mean 'static'?
[auth] src/models/user.ts(46,3): error TS2554: Expected 1 arguments, but got 0.
The statics property does exist in the schema object and it does show when I console.log(userSchema).静态属性确实存在于架构 object 中,并且在我 console.log(userSchema) 时确实显示。 I think it has something to do with kubernetes and skaffold.
我认为它与 kubernetes 和脚手架有关。 Any idea how to fix this problem??
知道如何解决这个问题吗?
I think this could help我认为这可能会有所帮助
First you have to create 3 interfaces.首先,您必须创建 3 个接口。
interface UserAttrs {
email: string;
password: string;
}
interface UserModel extends mongoose.Model<UserDoc> {
build(attrs: UserAttrs): UserDoc;
}
interface UserDoc extends mongoose.Document {
email: string;
password: string;
}
Then in your schema's middleware you have to declare the type of the variables that you're using然后在你的模式的中间件中,你必须声明你正在使用的变量的类型
userSchema.pre("save", async function (this: UserDoc, next: any) {
if (this.isModified("password")) {
const hashed = await Password.toHash(this.get("password"));
this.set("password", hashed);
}
next();
});
const User = mongoose.model<UserDoc, UserModel>('User', userSchema);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.