猫鼬架构钩和打字稿:属性不存在

时间:2020-04-28 19:22:02

标签: typescript mongoose

我正在尝试使用Schema中的Mongoose创建一个简单的用户typescript

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

const SALT_WORK_FACTOR = 10;

interface IUser extends Document {
  username: string;
  password: string;
  email: string;
  activeMembership: () => boolean;
  membershipExpires: Date;
  registeredOn: Date;
  lastLogin: Date;
  isAdmin: boolean;
}

const userSchema: Schema<IUser> = new Schema({
  username: { type: String, required: true },
  password: { type: String, required: true },
  email: { type: String, required: true },
  activeMembership: { type: Boolean, required: true, default: false },
  membershipExpires: { type: Number, required: true, default: 0 },
  registeredOn: { type: Date, required: true },
  lastLogin: { type: Date },
  isAdmin: { type: Boolean, required: true, default: false }
});

userSchema.pre("save", function (next) {
  const user = this;
  if (!user.isModified("password")) return next();

  bcrypt.genSalt(SALT_WORK_FACTOR, (err, salt) => {
    if (err) throw err;
    bcrypt.hash(user.password, salt, (err, hash) => {
      if (err) throw err;
      user.password = hash;
      next();
    });
  });
});

但是打字稿一直在抱怨:

tsserver says Property 'password' does not exist on type 'Document'.

在线:

bcrypt.hash(user.password, salt, (err, hash) => {

和:

      user.password = hash;

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

尝试使用常规分配function navratna() { var y=document.getElementById("navrat"); y.innerText="ahoj"; }

<body>
    <table border="1">
<tr>
<td height="20" width="100" id="navrat">
</td>
</tr>
</table>
<input

 type="button" value="pokus" onclick="navratna()"/>
</body>

希望有帮助!