简体   繁体   中英

Mongoose Methods and Typescript - this property is undefined

I'm developing a typescript app a restful api using node,express and mongoose.

I have an auth controller with a store function attached to POST: /api/auth . The user passes in their email and password to compare it against the hashed version.

However the comparePassword function in the use.model.ts doesent work because this.password is undefined.

auth.controller.ts

import { Request, Response, NextFunction } from 'express';

import User from './../users/user.model';
import jwt from 'jsonwebtoken';
import Config from './../../config/config';

class AuthController {

    private config: any;

    constructor() {

        this.config = new Config();

    }

    public async store(req: Request, res: Response): Promise<any> {

        const input = req.body;

        console.log(input);
        try {

            let user = await User.findOne({ 'email': input.email });

            if (!user) {
                throw {};
            }
            console.log(user);

            user.schema.methods.comparePassword(input.password, (err: any, isMatch: any) => {

                if (err || !isMatch) {

                    return res.status(401).json({
                        success: false,
                        status: 401,
                        data: { err, isMatch },
                        message: 'Authentication Failed, wrong password',
                    });

                }

                if (!err && isMatch) {

                    const token = jwt.sign({ sub: user._id }, this.config.jwt.secretOrKey);

                    return res.status(200).json({
                        success: true,
                        status: 200,
                        data: { user, token },
                        message: 'Authentication Successful',
                    })

                }


            })

        } catch (err) {

            return res.status(404).json({
                success: false,
                status: 404,
                data: err,
                message: 'Failed to Authenticate'
            })

        }


    }
}

export default AuthController;

user.model.ts

import { Schema, Model, Document, model } from 'mongoose';
import bcrypt from 'bcryptjs';
import { UserInterface } from './user.interface';


const UserSchema = new Schema({

    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },

}, {
        timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
    });


UserSchema.pre('save', function (next) {
    let user = <UserInterface>this;
    let SALT_WORK_FACTOR = 10;

    // only hash the password if it has been modified (or is new)
    if (!user.isModified('password')) return next();

    // generate a salt
    bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
        if (err) return next(err);

        // hash the password using our new salt
        bcrypt.hash(user.password, salt, function (err, hash) {
            if (err) return next(err);

            // override the cleartext password with the hashed one
            user.password = hash;
            next();
        });
    });
});

UserSchema.methods.comparePassword = function (candidatePassword: any, cb: any) {

    //let user = <UserInterface>this;

    console.log(candidatePassword);
    console.log(this.password);

    bcrypt.compare(candidatePassword, this.password, function (err, isMatch) {
        if (err) return cb(err);
        cb(null, isMatch);
    });
};



const User = model<UserInterface>('Users', UserSchema);

export default User;

I have similar comparePassword function working in another project that doesn't use typescript. So im not sure why "this" is undefined and is not set to the mongoose user object.

Here's what I do to bypass that typescript issue.

const self:any = this;

Usage example:

UserSchema.methods.comparePassword = function (candidatePassword: any, cb: any) {
    const self:any = this;

    console.log(candidatePassword);
    console.log(self.password);

    bcrypt.compare(candidatePassword, self.password, function (err, isMatch) {
        if (err) return cb(err);
        cb(null, isMatch);
    });
};

Also if you're in a rush and don't want to create an interface you can do this on your pre events.

UserSchema.pre<any>('save', function(next) {
   if (!this.isModified('password')) 
       return next();
   ...
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM