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

  • Home
  • SEARCH
  • 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 8038863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:22:41+00:00 2026-06-05T03:22:41+00:00

I have a model called Application: var ApplicationSchema = new mongoose.Schema({ name : {type:

  • 0

I have a model called Application:

var ApplicationSchema = new mongoose.Schema({
name       : {type: String, validate: [uniqueName, 'Unique Name']},
dateCreated: Date,
containers : [ContainerSchema]
});
mongoose.model('Application', ApplicationSchema);
var Application = database.model('Application');

It calls a validation function called uniqueName when it saves:

function uniqueName(name)
{
console.log('In Unique Name function');
Application.find({}, function(error, documents) {
    for(var i = 0; i < documents.length; i++) {
        if(documents[i].name == name) {
            console.log('About to return false');
            return false;
        }
    }
});
return true;
}

Later on in the code I put some data in the model and save it:

newApplication.name = request.body.name;
newApplication.save(function(error) {
    console.log('Callback for save');
    if(error) {
        console.log('error if statement');
        response.statusCode = 409;
        response.end();
    }
    console.log('Done with callback');
});
response.statusCode = 201;
response.end();

When I test this with a name that is not unique, I get a 201 response and the following output from my terminal:

In Unique Name function
Callback for save
Done with callback
About to return false

Am I doing something wrong, or is this really a race condition in Mongoose?

  • 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-05T03:22:43+00:00Added an answer on June 5, 2026 at 3:22 am

    When your validator is asynchronous (as it is here) it needs to accept a second parameter which is a callback that you must call with true or false, depending on whether that validation passed. So:

    function uniqueName(name, callback)
    {
        Application.find({}, function(error, documents) {
            for(var i = 0; i < documents.length; i++) {
                if(documents[i].name == name) {
                    return callback(false);
                }
            }
            return callback(true);
        }
    }
    

    However, this is not very efficient; you should either:

    Filter your find instead of getting all docs and manually searching them. e.g.

    Application.find({name: name} ...
    

    OR even better:

    Create a unique index on name and let mongo ensure uniqueness for you. e.g.

    var ApplicationSchema = new mongoose.Schema({
        name: {type: String, unique: true}, 
        ...
    
    • 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 Details, and two controller methods new and create. New
In my application I have an extended User model called UserProfile. This user can
I have a CodeIgniter MVC application. I have a model called city that has
In my application I have a model called Product. Every time a user adds
I have a model called Contact which has_one guest like so. class Contact <
I have a model called Answer which has a ForeignKey relationship to another model
I have a model called company that has_many users then users belongs_to company. class
I have a model called user and another model called student. Neither of them
I have a model called Customer in google app engine with python: class Customer(db.Model):
Say I have a model called User that has the following parameters: favorite_color, favorite_animal,

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.