madhums / node-express-mongoose-demo

A simple demo app using node and mongodb for beginners (with docker)
https://nodejs-express-demo.fly.dev
MIT License
5.12k stars 1.38k forks source link

User validation failed: email: fn is not a function #334

Closed astranavt closed 5 years ago

astranavt commented 6 years ago

in this place

UserSchema.path('email').validate(function (email, fn) {
    const User = mongoose.model('User');
    if (this.skipValidation()) fn(true);

    // Check only when it is a new user or when email field is modified
    if (this.isNew || this.isModified('email')) {
        User.find({
            email: email
        }).exec(function (err, users) {
            fn(!err && users.length === 0);
        });
    } else fn(true);
}, 'Email already exists');

what is the function of fn ?

MikeKa commented 5 years ago

it gives an error: events.js:160 throw er; // Unhandled 'error' event ^

TypeError: fn is not a function at /Users/mk/git/node-express-mongoose-demo/app/models/user.js:78:7 at /Users/mk/git/node-express-mongoose-demo/node_modules/mongoose/lib/model.js:4533:16 at (anonymous function).call.error (/Users/mk/git/node-express-mongoose-demo/node_modules/mongoose/lib/query.js:3800:7) at process.nextTick (/Users/mk/git/node-express-mongoose-demo/node_modules/mongoose/lib/helpers/query/completeMany.js:35:39) at _combinedTickCallback (internal/process/next_tick.js:67:7) at process._tickCallback (internal/process/next_tick.js:98:9)

madhums commented 5 years ago

Sorry for the late response, this should be fixed now. https://github.com/madhums/node-express-mongoose-demo/commit/996565700654522649ff798b7e638655e621efc4#diff-a4b3038d4ee7f4db64a479a6b17460a6