当前位置: 首页 > 面试题库 >

Typescript猫鼬静态模型方法“类型上不存在属性”

邢宏浚
2023-03-14
问题内容

我目前正在尝试向我的猫鼬模式中添加静态方法,但是我找不到它无法通过这种方式工作的原因。

我的模特:

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;

IUser:

export interface IUser {
    email: string;
    name: string;
    password: string;
}

如果我现在尝试拨打电话,User.hashPassword(password)则会出现以下错误[ts] Property 'hashPassword' does not exist on type 'Model<IUserModel>'.

我知道我没有在任何地方定义方法,但我真的不知道在哪里可以放置它,因为我不能只是将静态方法放到接口中。希望您能帮助我找到错误,在此先感谢!


问题答案:

我认为您遇到了我刚刚遇到的同样问题。您可以拨打此电话。几个教程让您.comparePassword()从这样的模型中调用方法。

User.comparePassword(candidate, cb...)

这是行不通的,因为该方法位于上,schema而不位于上model。我能够调用该方法的唯一方法是使用标准的mongoose /
mongo查询方法找到该模型的实例。

这是我的护照中间件的相关部分:

passport.use(
  new LocalStrategy({
    usernameField: 'email'
  },
    function (email: string, password: string, done: any) {
      User.findOne({ email: email }, function (err: Error, user: IUserModel) {
        if (err) throw err;
        if (!user) return done(null, false, { msg: 'unknown User' });
        user.schema.methods.comparePassword(password, user.password, function (error: Error, isMatch: boolean) {
          if (error) throw error;
          if (!isMatch) return done(null, false, { msg: 'Invalid password' });
          else {
            console.log('it was a match'); // lost my $HÏT when I saw it
            return done(null, user);
          }
        })
      })
    })
);

因此,我曾经findOne({})获取文档实例,然后不得不通过深入研究文档上的架构属性来访问架构方法
user.schema.methods.comparePassword

我注意到了几个区别:

  1. 我的是instance方法,而你的是static方法。我相信会有类似的方法访问策略。
  2. 我发现我必须将哈希传递给comparePassword()函数。也许这对静态变量不是必需的,但是我无法访问this.password


 类似资料:
  • 我使用的是完全修补过的Visual Studio 2013。我正在尝试使用JQuery、JQueryUI和JSRender。我也在尝试使用打字。在ts文件中,我得到如下错误: 类型“{}”上不存在属性“fade div”。

  • 我是一个使用Typescript的新手,我得到一个错误提示: 没有与此调用匹配的重载。重载1 of 2,'(道具:只读 类型“{注意:{ 1:{ _ id:number;标题:字符串;正文:字符串;updatedAt:日期;};};“}”不能赋给类型“IntrinsicAttributes” = = = = = = = = = = = = = = = = = = = = = = = = = = =

  • 问题内容: 我有一个让我感到困惑的奇怪问题。我有一个模型: 变体条目如下所示: 我需要添加一个新字段-说“颜色”。所以我这样做是为了批量更新: 但是,“颜色”字段未设置-如果我再次浏览并注释掉该行,则它不会显示。我似乎无法弄清楚为什么要这样做。我有一个onSave事件,该事件已正确触发,因此可以保存。我也没有对版本结构进行任何检查- 即没有代码只允许代码和价格。我显然缺少了一些东西,但几个小时后我

  • 我已将TS添加到我的React/Redux应用程序中。 我在我的应用程序中使用对象,如下所示: TS抛出一个错误: 类型脚本错误:类型“窗口”上不存在属性“FB”。TS2339 我想修正这个错误。 我在这里找到了答案https://stackoverflow.com/a/56402425/1114926 为什么第一个版本不起作用,而第二个版本起作用,尽管我根本没有指定FB属性?

  • 在前端,我有3个层次结构。 顶层显示一个测试摘要,包含统计信息,如%通过/失败、各种标签、运行标识等。 第二级由测试套件的各个部分组成,这些部分可以通过顶级运行id访问。其中包含测试的名称,特定的测试通过/失败。 第三级是实际测试本身,包括报告、通过/失败状态等。 我使用的是平均堆栈,我想知道使用1、2或3种不同的Mongoose模型在MongoDB中存储数据的利弊。我知道在数组中使用嵌入式文档的

  • 问题内容: 在阅读教程时,通常会在模式和模型之间进行区分,特别是在处理mongoose / mongodb时。由于在该系统下似乎不存在“模型”,因此移植到Postgresql会有些混乱。两种方法有什么区别? 例如,此行的postgres / sql ORM等价于什么? (猫鼬和express.js): 问题答案: 在猫鼬中,模式表示特定文档的结构,可以是完整文档,也可以是文档的一部分。这是表达期望