Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:43:04+00:00 2026-06-15T02:43:04+00:00

Following up from : Mongoose unique validation error type I’m using this schema with

  • 0

Following up from : Mongoose unique validation error type

I’m using this schema with mongoose 3.0.3 from npm:

var schema = new Schema({

    _id: Schema.ObjectId,
    email: {type: String, required: true, unique: true}

});

With this middleware to get a validationError from unique:true

schema.pre("save", function(next, done) {
    var self = this;

    model.findOne({email : this.email}, 'email', function(err, results) {
        if(err) {
            done(err);
        } else if(results) {
            console.warn('results', results);
            self.invalidate("email", "email must be unique");
            done(new Error("email must be unique"));
        } else {
            done();
        }
    });

    next();
});

However, it does not work!

Users.create() will still return a a MongoError: E11000 duplicate key error index and the console.warn() is only called after that.

The save action should not be called until all middleware done()have been called according to the docs, and I should get back a validation error.

It looks like the done() behavior is not working as expected,

Any idea why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:43:05+00:00Added an answer on June 15, 2026 at 2:43 am

    You’re using a parallel middleware callback function (with both next and done), but you’re not setting the parallel flag in the schema.pre parameters so it’s using the serial rules.

    So either include the parallel flag in your call:

    schema.pre("save", true, function(next, done) { ...
    

    Or switch to a serial middleware callback style if that’s all you need anyway:

    schema.pre("save", function(next) {
        var self = this;
    
        model.findOne({email : this.email}, 'email', function(err, results) {
            if(err) {
                next(err);
            } else if(results) {
                console.warn('results', results);
                self.invalidate("email", "email must be unique");
                next(new Error("email must be unique"));
            } else {
                next();
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called Application: var ApplicationSchema = new mongoose.Schema({ name : {type:
I'm doing the following using Mongoose: that.model.update({_id: dao._id}, dao, { upsert: true }, cb);
I have the following 2 schemas: Company Event: var companyEventSchema = new Schema({ name
Following from this: Every specific weekday, sql and php I got this: deals_bookings.everyWeekDay =
Is the following From header incorect? // To send HTML mail, the Content-type header
I've extracted the following from a project we are working on: http://www.bridgedev1.com/videotest.html This plays
I'm trying to add the validation and get the following from the compiler. The
I'm trying to write a sql query to the following (from this site http://sqlzoo.net/1b.htm
I have the following from the server response: {invalid_emails:[adsasdasd],result:success,valid_emails:[jobs@apple.com]} But this errors? $.ajax({ type:
We are saving the following from one application: final CheckBoxPreference mCheckPref = new CheckBoxPreference(sContext);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.