0

According to the error-trace the error occurs in the "validate" method, but as far as i see it my compare call is correct. I hope someone can explain to me why it happens anyways.

POST /user/register 500 23.312 ms - 2235

Error: data and hash arguments required at Object.compare at model.user_schema.methods.validate

The mongoose model:

const mongoose = require('mongoose');
const bcrypt = require('bcrypt');

const salty = 10;

const user_schema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        unique: false,
        minlength: 3,
        maxlength: 32
    },
    email: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    }
});

user_schema.pre('save', (next) => {
    // if the password is not changed, there is no need to hash it again
    if (!this.isModified('password')) return next();
    // hash the user password
    bcrypt.hash(this.password, salty, (err, hash) => {
        if (err) return next(err);
        this.password = hash;
        return next();
    });
});

user_schema.methods.validate = (claim, callback) => {
    // compare the password to the existing hash from the database
    bcrypt.compare(claim, this.password, (err, is_match) => {
        if (err) return callback(err);
        return callback(null, is_match);
    });
}

module.exports = mongoose.model('user', user_schema);

The router with the create call:

router.post('/register', (req, res, next) => {
    let new_user = {
        name: req.body.name,
        email: req.body.email,
        password: req.body.password
    };
    user_model.create(new_user, (err, user) => {
        if (err) return next(err);
        res.send(user);
    });
});
JakobSch
  • 13
  • 1
  • 5
  • Are "claim" and "this.password" both defined? What if it's the first time setting/creating the user and there is no existing password? – AdamExchange Dec 18 '20 at 16:26
  • @AdamExchange this.password is required as i defined in the schema. The and i don't call validate, i just create the model. – JakobSch Dec 18 '20 at 16:32
  • Saving will automatically call `validate`. It appears that either claim or this.password is undefined when it is being called. – AdamExchange Dec 18 '20 at 16:52
  • @AdamExchange so it turns out, the claim is undefined. Which makes sense, because i never called this method. Any idea how to fix it? – JakobSch Dec 18 '20 at 17:02
  • Is "validate" really automatically called? it is not being called in the 'save' pre hook. My guess was to check the req.body to make sure the password in the user object is valid. – Trevor Njeru Dec 18 '20 at 17:31
  • @TrevorNjeru the http body is ok – JakobSch Dec 18 '20 at 17:43
  • Document.validate is automatically called before save/create. I would rename the method as `check_token` or `compare_claim` or something other than validate so it won't run automatically. – AdamExchange Dec 18 '20 at 17:52
  • @AdamExchange thank you, i renamed the function and everything is fine – JakobSch Dec 18 '20 at 18:24

0 Answers0