Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

298
Vistas
user.save() is not a function when i am trying to save the model
  const user = User.findOne({email:req.body.email});
    
      if(!user){
        return next(new ErrorHander("User not found",404));
    
      }
 const resetToken = user.getResetPasswordToken

  await user.save({ validateBeforeSave: false });
  

I dont know why user.save is not working in mern mongodb i have same peice of code working for other person but not for me

What i did is that i get the user by fireing the query in the line one after having that user i should be able to use user.save() function here but sadly it is giving error

//here is my model schema

const mongoose = require("mongoose");
const validator = require("validator");
const bcrypt = require("bcryptjs");
const crypto = require("crypto")

const jwt = require("jsonwebtoken");

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, "please Ente the your name"],
  },
  email: {
    type: String,
    required: [true, "Please Enter the email"],
    unique: [true],
    validate: [validator.isEmail, "Please Enter a valid email"],
  },
  password: {
    type: String,
    required: true,
    minlength: [8, "Passwaord must be more that 8 characters"],
    select: false,
  },
  avatar: {
    public_id: {
      type: String,
      required: true,
    },
    url: {
      type: String,
      required: true,
    },
  },
  role: {
    type: String,
    default: "user",
  },
  resetPasswordToken: String,
  resetPasswordTokenExpire: Date,

  createdAt: {
    type: Date,
    default: Date.now,
  },
});

userSchema.pre("save", async function (next) {
  if (!this.isModified("password")) {
    next();
  }

  this.password = await bcrypt.hash(this.password, 10);
});

userSchema.methods.comparePassword = async function (password) {
  return await bcrypt.compare(password, this.password);
};

userSchema.methods.getJWTToken = function () {
  return jwt.sign({ id: this._id }, process.env.JWT_SECRET, {
    expiresIn: process.env.JWT_EXPIRE,
  });
};

// compare password

// userSchema.methods.comparePassword = async function(enterPass){
//     return await bcyrypt.compare(enterPass,this.password)
// }

// Reset Password

userSchema.methods.getResetPasswordToken = function(){
  const resetToken = crypto.randomBytes(20).toString("hex");
  this.resetPasswordToken = crypto.createHash("sha256").update(resetToken).digest("hex");
  this.resetPasswordTokenExpire = Date.now +15*60*1000;

  return resetToken;
}

module.exports = mongoose.model("User", userSchema);
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

User.findOne will return a Promise. So you need to add await keyword.

const user = await User.findOne({email:req.body.email});
    
      if(!user){
        return next(new ErrorHander("User not found",404));
    
      }
 const resetToken = user.getResetPasswordToken

  await user.save({ validateBeforeSave: false });
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda