我目前正在尝试向我的 mongoose 架构添加一个静态方法,但我找不到它不能以这种方式工作的原因。
我的模特:
import * as bcrypt from 'bcryptjs';
import { Document, Schema, Model, model } from 'mongoose';
import { IUser } from '../interfaces/IUser';
export interface IUserModel extends IUser, Document {
comparePassword(password: string): boolean;
}
export const userSchema: Schema = new Schema({
email: { type: String, index: { unique: true }, required: true },
name: { type: String, index: { unique: true }, required: true },
password: { type: String, required: true }
});
userSchema.method('comparePassword', function (password: string): boolean {
if (bcrypt.compareSync(password, this.password)) return true;
return false;
});
userSchema.static('hashPassword', (password: string): string => {
return bcrypt.hashSync(password);
});
export const User: Model<IUserModel> = model<IUserModel>('User', userSchema);
export default User;
用户:
export interface IUser {
email: string;
name: string;
password: string;
}
如果我现在尝试调用 User.hashPassword(password)
我收到以下错误 [ts] Property 'hashPassword' does not exist on type 'Model<IUserModel>'.
我知道我没有在任何地方定义方法,但我真的不知道我可以把它放在哪里,因为我不能只将静态方法放入接口(interface)中。 希望您能帮我找出错误,在此先感谢!
最佳答案
我遇到了和你一样的问题,然后在阅读了 TS mongoose typings 中的文档后终于设法解决了它(我以前不知道,我不确定文档已经存在了多长时间左右),特别是 this section .
至于您的情况,您需要遵循与您目前所拥有的类似的模式,尽管您需要在两个文件中更改一些内容。
IUser 文件
- 重命名
IUser
至IUserDocument
.这是为了将您的架构与您的实例方法分开。 - 导入
Document
来自 Mongoose 。 - 从
Document
扩展接口(interface).
模型文件
- 重命名
IUser
的所有实例至IUserDocument
,如果重命名文件,则包括模块路径。 - 重命名仅定义
IUserModel
至IUser
. - 更改内容
IUser
延伸自,从IUserDocument, Document
至IUserDocument
. - 创建一个名为
IUserModel
的新接口(interface)从Model<IUser>
延伸. - 在
IUserModel
中声明您的静态方法. - 更改
User
来自Model<IUserModel>
的常量类型至IUserModel
,如IUserModel
现在扩展Model<IUser>
. - 从
<IUserModel>
更改模型调用的类型参数至<IUser, IUserModel>
.
以下是您的模型文件经过这些更改后的样子:
import * as bcrypt from 'bcryptjs';
import { Document, Schema, Model, model } from 'mongoose';
import { IUserDocument } from '../interfaces/IUserDocument';
export interface IUser extends IUserDocument {
comparePassword(password: string): boolean;
}
export interface IUserModel extends Model<IUser> {
hashPassword(password: string): string;
}
export const userSchema: Schema = new Schema({
email: { type: String, index: { unique: true }, required: true },
name: { type: String, index: { unique: true }, required: true },
password: { type: String, required: true }
});
userSchema.method('comparePassword', function (password: string): boolean {
if (bcrypt.compareSync(password, this.password)) return true;
return false;
});
userSchema.static('hashPassword', (password: string): string => {
return bcrypt.hashSync(password);
});
export const User: IUserModel = model<IUser, IUserModel>('User', userSchema);
export default User;
还有你的(新改名的)../interfaces/IUserDocument
模块看起来像这样:
import { Document } from 'mongoose';
export interface IUserDocument extends Document {
email: string;
name: string;
password: string;
}
关于node.js - Typescript mongoose 静态模型方法 "Property does not exist on type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42448372/