node.js - Typescript mongoose 静态模型方法 "Property does not exist on type"

标签 node.js typescript mongoose

我目前正在尝试向我的 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 文件

  1. 重命名 IUserIUserDocument .这是为了将您的架构与您的实例方法分开。
  2. 导入 Document来自 Mongoose 。
  3. Document 扩展接口(interface).

模型文件

  1. 重命名 IUser 的所有实例至IUserDocument ,如果重命名文件,则包括模块路径。
  2. 重命名仅定义 IUserModelIUser .
  3. 更改内容IUser延伸自,从 IUserDocument, DocumentIUserDocument .
  4. 创建一个名为 IUserModel 的新接口(interface)从 Model<IUser> 延伸.
  5. IUserModel 中声明您的静态方法.
  6. 更改User来自 Model<IUserModel> 的常量类型至IUserModel ,如 IUserModel现在扩展 Model<IUser> .
  7. <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/

相关文章:

node.js - NodeJS - 如何控制 Promise 的执行流程?

node.js - Google App Engine 连接.session() 错误

javascript - Angular 5 中的 Typescript 继承

javascript - Mongoose 如何很好地处理密码编码?

javascript - 我无法提取 JSON 格式的某些数据

node.js - connect.js 中间件的正确顺序?

javascript - TypeError : this. props.nextStep 不是函数 React 错误

Angular2 向 DOM 树发出事件

node.js - 在 mongoose (mongodb) 中设置集合属性的到期时间

javascript - MongooseJS/MongoDB 搜索精确短语