小编Dan*_*sky的帖子

使用 TypeScript 创建猫鼬模式方法

我尝试为用户模式创建 hashPassword 方法。

schema.method("hashPassword", function (): void {
  const salt = bcrypt.genSaltSync(10);
  const hash = bcrypt.hashSync(this.password, salt);

  this.password = hash;
});
Run Code Online (Sandbox Code Playgroud)

Property 'password' does not exist on type 'Document<any>'.并收到密码错误

这是我的文件

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

/**
 * This interface should be the same as JWTPayload declared in types/global.d.ts file
 */
export interface IUser extends Document {
  name: string;
  email: string;
  username: string;
  password: string;
  confirmed: boolean;
  hashPassword: () => void;
  checkPassword: (password: …
Run Code Online (Sandbox Code Playgroud)

mongoose node.js typescript

7
推荐指数
3
解决办法
8626
查看次数

标签 统计

mongoose ×1

node.js ×1

typescript ×1