Home > Mobile >  Argument of type '"save"' is not assignable to parameter of type 'RegExp |
Argument of type '"save"' is not assignable to parameter of type 'RegExp |

Time:12-27

I am using Mongoose to try and create a user model for my application. I have consulted their documentation and have come up with the following code.

import { NextFunction } from 'express';
import { Schema, model } from 'mongoose';
const bcrypt = require("bcrypt")

export interface IUser {
    email: string
    password: string
    username: string
}

const UserSchema = new Schema<IUser>(
    {
        email: { type: String, required: true },
        password: { type: String, required: true, minlength: 5, hide: true },
        username: { type: String, required: true, minlength: 2 },
    },
)

UserSchema.pre("save", async function(next: NextFunction) {
    const user = this;
    if(!user.isModified("password")){
        next();
    }
    bcrypt.genSalt(10, (err: Error, salt: string) => {
        if(err) {
            return next(err);
        }
        bcrypt.hash(user.password, salt, (err: Error, hash: string) => {
            if(err){
                return next(err);
            }
            user.password = hash;
            next();
        })
    })
});

UserSchema.methods.comparePassword = function (password: string, cb: Function) {
    const user = this;
    bcrypt.compare(password, user.password, (err: Error, isMatch: boolean) => {
        if(err) {
            return cb(err);
        }
        cb(null, isMatch);
    })
}

const User = model<IUser>("User", UserSchema);
export default User

Everything is fine except the part where I try to insert a pre hook for when saving a document. I get a red underline under "save" that says

No overload matches this call.
  The last overload gave the following error.
    Argument of type '"save"' is not assignable to parameter of type 'RegExp | "insertMany"'.ts(2769)

that I don't understand. I have Googled around for a while and everywhere I look, people write the hook in identical ways. What am I doing wrong?

I found this post on here about pretty much the same problem, but it did not help.

CodePudding user response:

The problem has been solved. The problem is that I was using the NextFunction type from express to annotate the next parameter in the callback function.

Changing

UserSchema.pre('save', async function(next: NextFunction) {
    const user = this;
    
    if(!user.isModified("password"))
        next();
    
    bcrypt.genSalt(10, (err: Error, salt: string) => {
        if(err) return next(err);
        bcrypt.hash(user.password, salt, (err: Error, hash: string) => {
            if(err) return next(err);
            user.password = hash;
            next();
        })
    })
});

to

UserSchema.pre('save', async function(next) {
    const user = this;
    
    if(!user.isModified("password"))
        next();
    
    bcrypt.genSalt(10, (err: Error, salt: string) => {
        if(err) return next(err);
        bcrypt.hash(user.password, salt, (err: Error, hash: string) => {
            if(err) return next(err);
            user.password = hash;
            next();
        })
    })
});

solves the problem. I don't know what the correct type is, but will be looking into it. The problem is fixed nonetheless.

  • Related