Skip to content

MongooseError: Model.prototype.save() no longer accepts a callback #60

@sajadhn1988

Description

@sajadhn1988

would you please revise this code to work again:
`const mongoose = require("mongoose");
const crypto = require("crypto");
const uuid = 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: true,
},
hashed_password: {
type: String,
required: true,
},
about: {
type: String,
trim: true,
},
salt: String,
role: {
type: Number,
default: 0,
},
history: {
type: Array,
default: [],
},
},
{ timestamps: true }
);

// virtual field
userSchema
.virtual("password")
.set(function (password) {
this._password = password;
this.salt = uuid();
this.hashed_password = this.encryptPassword(password);
})
.get(function () {
return this._password;
});

userSchema.methods = {
authenticate: function (plainText) {
return this.encryptPassword(plainText) === this.hashed_password;
},

encryptPassword: function (password) {
if (!password) return "";
try {
return crypto
.createHmac("sha1", this.salt)
.update(password)
.digest("hex");
} catch (err) {
return "";
}
},
};

module.exports = mongoose.model("User", userSchema);`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions