在'Document <any>类型上不存在猫鼬属性'password'

驼鹿哈米德

-userSchema.ts接口

import mongoose, { Schema, Document } from "mongoose";
import moment from "moment";
import bcrypt from "bcrypt";

export interface UserDoc extends Document {
  name: {
    type: string;
    required: boolean;
  };
  email: {
    type: string;
    required: boolean;
  };
  password: {
    type: string;
    required: boolean;
  };
  dateJoined: {
    type: string;
    default: string;
  };
}

const userSchema = new Schema({
  name: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },

  dateJoined: {
    type: String,
    default: moment().format("MMMM Do YYYY"),
  },
});

我创建了用户模型,而我遇到的问题是使用bcrypt来创建matchPassword方法,以比较EnteredPassword参数与数据库中的密码

userSchema.methods.matchPassword = async function (enteredPassword) {
  return await bcrypt.compare(enteredPassword, this.password); ***
};

userSchema.pre("save", async function (next) {
  if (this.isModified("password")) {
    next();
  }

  const salt = bcrypt.genSalt(10);

  *** this.password = await bcrypt.hash(this.password, await salt); ***
});

const User = mongoose.model("User", userSchema);

错误消息如下:

Property 'password' does not exist on type 'Document<any>'.

并且此错误出现在***突出显示this.password的每个实例上

我以前在Javascript中使用过相同的方法,所以我不知道为什么它在打字稿上不起作用,以及如何将this.password绑定到Mongoose文档上

谢谢

可爱的女皇

看来@Mohammad已经帮助您bcrypt实现了。我可以为您解决打字稿错误!

UserDoc是一个打字稿界面,因此不应包含required它应该只描述UserDoc对象的类型默认情况下,属性是必需的。我们使用?:它们是否是可选的,但看起来这里是所有必需的。

export interface UserDoc extends Document {
  name: string;
  email: string;
  password: string;
  dateJoined: string;
  matchPassword: (pw: string) => Promise<boolean>
}

在创建时userSchema通过将构造函数的泛型变量设置为,可以告诉打字稿这是a的架构,UserDoc而不仅仅是any的架构DocumentSchemaUserDoc

const userSchema = new Schema<UserDoc>({ ...

userSchema.methods.matchPassword由于我们知道这this.password是个错误,因此可以清除中的错误string我们也知道那enteredPasswordstring因为我们matchPasswordUserDoc接口中为此方法定义了参数

由于某种原因,该pre方法无法自动识别我们的文档类型。但是该pre方法本身是一个泛型函数,因此我们可以再次指定doc是一个UserDoc

userSchema.pre<UserDoc>( ...

这很愚蠢,但是我们在创建模型时必须再次指定泛型

const User = mongoose.model<UserDoc>("User", userSchema);

现在User具有type,mongoose.Model<UserDoc, {}>并且您调用的所有方法都应该返回aUserDoc而不是a Document

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

类型“ Document <any>”上不存在属性“ password”

来自分类Dev

猫鼬TypeScript聚合:类型'any []'不存在

来自分类Dev

类型“ Marker <any>”上不存在属性“ _popup”

来自分类Dev

类型“ Marker <any>”上不存在属性“ _popup”

来自分类Dev

类型“ FirebaseListObservable <any []>”上不存在属性“ map”

来自分类Dev

类型“Observable<any>”上不存在属性“interval”

来自分类Dev

订阅 FirebaseListObservable - 类型 any[] 上不存在属性 $key

来自分类Dev

类型“EventEmitter<any>”上不存在属性“custom”

来自分类Dev

类型“any[]”上不存在属性“id”

来自分类Dev

猫鼬:属性“拉”在类型“地址[]”上不存在

来自分类Dev

angular2的Observable属性'debouceTime'在'Observable <any>'类型上不存在

来自分类Dev

在ng build--aot期间,类型“ any []”上不存在属性“名称”

来自分类Dev

Angular 11的问题-带有RXJS的promise <any>类型上不存在属性名称

来自分类Dev

类型 any[] 上不存在 Angular4 和 TypeScript 属性

来自分类Dev

Ionic3、AngularFire - 类型“Observable<any[]>”上不存在属性“remove”

来自分类Dev

Angular 5 - 类型 '() => Observable<any>' 上不存在属性 'subscribe'

来自分类Dev

错误显示在我的 .ts 代码中 > [ts] 类型“any[]”上不存在属性“name”

来自分类Dev

类型'AxiosResponse <any>'不存在属性'map'.ts(2339)

来自分类Dev

TS2345 [错误]:类型'{深度:和属性'响应'的参数在类型'{respnse:any;上不存在。}'在Deno上编译

来自分类Dev

在Angular中,类型“ OperatorFunction <any,any>”错误不存在获取属性“过滤器”

来自分类Dev

打字稿错误:'Observable <any>'类型不存在属性'value'

来自分类Dev

使用Typescript类型错误反应“自定义挂钩”“类型'interface |(({{target}:any)=> void)'。ts(2339)上不存在属性'x'

来自分类Dev

nodejs猫鼬错误TS2551:类型“文档”上不存在属性“ isDeleted”。您的意思是“ $ isDeleted”

来自分类Dev

Observable <Any>触发TSLint'属性不存在'

来自分类Dev

TypeScript 错误:对象字面量只能指定已知属性,并且类型“DeepPartial<Document>”中不存在“...”

来自分类Dev

猫鼬-this.find()不存在

来自分类Dev

猫鼬更新了不存在的文档

来自分类Dev

属性“ then”在类型“ void”上不存在

来自分类Dev

类型“ {}”上不存在属性“ count”

Related 相关文章

  1. 1

    类型“ Document <any>”上不存在属性“ password”

  2. 2

    猫鼬TypeScript聚合:类型'any []'不存在

  3. 3

    类型“ Marker <any>”上不存在属性“ _popup”

  4. 4

    类型“ Marker <any>”上不存在属性“ _popup”

  5. 5

    类型“ FirebaseListObservable <any []>”上不存在属性“ map”

  6. 6

    类型“Observable<any>”上不存在属性“interval”

  7. 7

    订阅 FirebaseListObservable - 类型 any[] 上不存在属性 $key

  8. 8

    类型“EventEmitter<any>”上不存在属性“custom”

  9. 9

    类型“any[]”上不存在属性“id”

  10. 10

    猫鼬:属性“拉”在类型“地址[]”上不存在

  11. 11

    angular2的Observable属性'debouceTime'在'Observable <any>'类型上不存在

  12. 12

    在ng build--aot期间,类型“ any []”上不存在属性“名称”

  13. 13

    Angular 11的问题-带有RXJS的promise <any>类型上不存在属性名称

  14. 14

    类型 any[] 上不存在 Angular4 和 TypeScript 属性

  15. 15

    Ionic3、AngularFire - 类型“Observable<any[]>”上不存在属性“remove”

  16. 16

    Angular 5 - 类型 '() => Observable<any>' 上不存在属性 'subscribe'

  17. 17

    错误显示在我的 .ts 代码中 > [ts] 类型“any[]”上不存在属性“name”

  18. 18

    类型'AxiosResponse <any>'不存在属性'map'.ts(2339)

  19. 19

    TS2345 [错误]:类型'{深度:和属性'响应'的参数在类型'{respnse:any;上不存在。}'在Deno上编译

  20. 20

    在Angular中,类型“ OperatorFunction <any,any>”错误不存在获取属性“过滤器”

  21. 21

    打字稿错误:'Observable <any>'类型不存在属性'value'

  22. 22

    使用Typescript类型错误反应“自定义挂钩”“类型'interface |(({{target}:any)=> void)'。ts(2339)上不存在属性'x'

  23. 23

    nodejs猫鼬错误TS2551:类型“文档”上不存在属性“ isDeleted”。您的意思是“ $ isDeleted”

  24. 24

    Observable <Any>触发TSLint'属性不存在'

  25. 25

    TypeScript 错误:对象字面量只能指定已知属性,并且类型“DeepPartial<Document>”中不存在“...”

  26. 26

    猫鼬-this.find()不存在

  27. 27

    猫鼬更新了不存在的文档

  28. 28

    属性“ then”在类型“ void”上不存在

  29. 29

    类型“ {}”上不存在属性“ count”

热门标签

归档