Varis Darasirikul
Varis Darasirikul

Reputation: 3063

Got undefined is not a function when tried to use methods function by node.js

i am working on the authenticate function by node.js. So when i tried to use methods.comparePassword function that i made on top to validate the password that user put on the form, i got an error but can't figure out why?

first i have UserSchema like this.

// user schema
var UserSchema = new Schema({
        name: String,
        username: {
            type: String,
            required: true,
            index: {
                unique: true
            }
        },
        password: {
            type: String,
            required: true,
            select: false
        }
});

var User = mongoose.model('User', UserSchema); 

Then i created the methods to comparePassword like this.

// method to compare a given password with the database hash
UserSchema.methods.comparePassword = function(password) {
    var user = this;

    return bcrypt.compareSync(password, user.password);
};

Then i made the route to authenticate and generate the token to users like this.

apiRouter.post('/authenticate',function(req,res){
    // find the user
    // select the name username and password explicitly
    User.findOne({
        username: req.body.username
    }).select('name username password').exec(function(err, user) {

        if(err) throw err;

        // no user with that username was found
        if(!user){
            res.json({
                success: false,
                message: 'Authentication failed. User not found.'
            });
        } else if(user){

            // check if password matches

            //console.log(req.body.password);
            var validPassword = user.comparePassword(req.body.password);
            //var validPassword = true; If i use this everything works fine.


            if (!validPassword) {
                res.json({
                    success: false,
                    message: 'Authentication failed. Wrong password.'
                });
            } else {

                // if user is found and password is right
                // create a token
                var token = jwt.sign({
                    name: user.name,
                    username: user.username
                }, superSecret, {
                    expiresInMinutes: 1440 // expires in 24 hours
                });

                // return the information including token as JSON
                res.json({
                    success: true,
                    message: 'Enjoy your token!',
                    token: token
                });
            }
        }

    });
});

But when i send the request to the server i got an error like this.

var validPassword = user.comparePassword(req.body.password);
                                  ^
TypeError: undefined is not a function
    at Promise.<anonymous> 

Then when i changed var validPassword = true; Everything works fine.

Anyone know how to fix this?

Thanks!

Upvotes: 0

Views: 391

Answers (1)

Kevin B
Kevin B

Reputation: 95023

Make sure you define these methods after creating the schema, but before creating the model.

// user schema
var UserSchema = new Schema({
    name: String,
    username: {
        type: String,
        required: true,
        index: {
            unique: true
        }
    },
    password: {
        type: String,
        required: true,
        select: false
    }
});

// method to compare a given password with the database hash
UserSchema.methods.comparePassword = function(password) {
    var user = this;

    return bcrypt.compareSync(password, user.password);
};

var User = mongoose.model('User', UserSchema);

Upvotes: 1

Related Questions