Problems with TS - Mongoose

0

I am updating my MEAN API for Typescript, when I try to pass the bcrypt to generate the hash the compiler is pointing the following error

src/models/User.ts(57,12): error TS2339: Property 'password' does not exist on type 'Document'.

I do not know how to solve, the tutorials I see there are the same as mine, where am I wrong?

  import * as mongoose from "mongoose";
  import * as bcrypt from "bcrypt";
  import * as jwt from "jsonwebtoken";

  const Schema = mongoose.Schema;
  let saltRounds = 10;

  var validateEmail = function(email) {
    var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    return re.test(email);
  };

  export const UserSchema = new Schema({
    username: {
      unique: [true, "this username already exists"],
      type: String,
      required: [true, "you must enter a valid username"],
      minlength: [5, "you must enter at least 5 characters for this field"]
    },
    email: {
      type: String,
      required: false,
      trim: true,
      lowercase: true,
      index: true,
      unique: true,
      sparse: true,
      validate: [validateEmail, "Please fill a valid email address"]
    },
    password: { type: String, required: true },
    nickname: String,
    preference: String,
    age: String,
    country: String,
    type: {
      type: Number,
      required: false,
      index: true,
      default: 0
    }
  });

  UserSchema.pre("save", function(next) {
    bcrypt.hash(this.password, saltRounds, (err, hash) => {
      this.password = hash;
      next();
    });
  });

this is the package

    "dependencies": {
      "bcrypt": "^2.0.1",
      "body-parser": "^1.18.3",
      "cors": "^2.8.4",
      "express": "^4.16.3",
      "express-jwt": "^5.3.1",
      "express-router": "0.0.1",
      "jsonwebtoken": "^8.3.0",
      "mongodb": "^3.1.6",
      "mongoose": "^5.2.16",
      "mongoose-paginate": "^5.0.3",
      "npm": "^6.4.1",
      "passport": "^0.4.0",
      "passport-local": "^1.0.0"
    },
    "devDependencies": {
      "@types/bcryptjs": "^2.4.2",
      "@types/express": "^4.16.0",
      "@types/mongodb": "^3.1.9",
      "@types/mongoose": "^5.2.0",
      "@types/passport": "^0.4.6",
      "morgan": "^1.9.1",
      "nodemon": "^1.17.5",
      "ts-node": "^7.0.0",
      "typescript": "^2.9.2"
    }
    
asked by anonymous 27.09.2018 / 16:02

0 answers