Home > Software engineering >  mongoose hashed_password is required
mongoose hashed_password is required

Time:11-09

Hello Community!

I having this issue with the node.js crypto.

Im trying to run some tests with postman, to see if my Database is working and unfortunately isnt working i dont know why.

I send it through json on postman:

this is just an example

{ "name": "Ryan", "email": "[email protected]", "password": "202020" }

Here is the code

const mongoose = require("mongoose");
const crypto = require("crypto");
const uuidv1 = require("uuid").v1;

const userSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      trim: true,
      required: true,
      maxlength: 32,
    },
    email: {
      type: String,
      trim: true,
      required: true,
      unique: 32,
    },
    hashed_password: {
      type: String,
      required: true,
    },
    about: {
      type: String,
      trim: true,
    },
    salt: {
      type: String,
    },
    role: {
      type: Number,
      default: 0,
    },
    history: {
      type: Array,
      default: [],
    },
  },
  { timestamps: true }
);

// Virtual field

userSchema
.virtual("password")
.set((password) => {
userSchema._password = password;
userSchema.salt = uuidv1();
//console.log(userSchema.methods.encryptPassword(password));
userSchema.hashed_password = userSchema.methods.encryptPassword(password);
})
.get(() => {
return userSchema._password;
});
// Encrypt the Password
userSchema.methods = {
encryptPassword: function (password) {
if (!password) {
  return "Doesnt have password";
}
try {
  return crypto
    .createHmac("sha1", userSchema.salt)
    .update(password)
    .digest("hex");
} catch (e) {
  console.log(typeof password, password);
  console.log(typeof userSchema.salt, userSchema.salt);
  return e;
}
},
};

And here is the log:

{
    "err": {
        "errors": {
            "hashed_password": {
                "name": "ValidatorError",
                "message": "Path `hashed_password` is required.",
                "properties": {
                    "message": "Path `hashed_password` is required.",
                    "type": "required",
                    "path": "hashed_password"
                },
                "kind": "required",
                "path": "hashed_password"
            }
        },
        "_message": "User validation failed",
        "name": "ValidationError",
        "message": "User validation failed: hashed_password: Path `hashed_password` is required."
    }
}

You might ask me why you use "userSchema" and not "this." I don't know why it doesn't recognize it

Anyways I don't know why it is saying "hashed_password" is required, since its receiving it from userSchema.methods.encryptPassword(password) function...

If someone know I would really appreciate it!

Thanks in advance.

CodePudding user response:

encryptPassword method is good. But change hashed_password field as password Also, use pre instead of virtual. Like that

userSchema.pre('save', async function(next) {
  this.password = await userSchema.methods.encryptPassword(this.password);
  next();
});

Virtual used for computed properties on docs. But you just want to crypto password before save in db. So change this with virtual part.

  • Related