ARTICLE AD BOX
I am working on one of my personal project and using Mongoose with TypeScript and trying to add a pre("save") middleware to hash password. But I am getting the following error
I am using Node.js Express with TypeScript
I tried by making next type as any but the save did not worked as expected
import {Schema, model, } from "mongoose"; import bcrypt from "bcryptjs"; import jwt from "jsonwebtoken"; import { IUser } from "../utils/types"; const UserSchema= new Schema({ firstName : { type : String, required : true, }, lastName : { type : String, required : true, }, mobileNumber : { type : Number, required :true }, email : { type : String, required : true, unique : true, }, password : { type : String, required : true, }, profilePhoto : { type : String, } },{ timestamps : true }); UserSchema.pre("save", async function (next ) { const user = this; if (!user.isModified("password")) { console.log("Password not modified, skipping hash"); return next(); } try { // hash password here const salt = await bcrypt.genSalt(10); const hashedPassword = await bcrypt.hash(user.password, salt); user.password = hashedPassword; console.log('Hashed password:', user.password); next(); } catch (err) { next(err as Error); } }); const UserSchemaModel = model("users", UserSchema); export default UserSchemaModel; Following is the Error This expression is not callable. Type 'SaveOptions' has no call signatures.