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 8340857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:17:50+00:00 2026-06-09T05:17:50+00:00

I am attempting to break out of a function based on an expression, however

  • 0

I am attempting to break out of a function based on an expression, however am having trouble with the scoping. Here is a snippet of the code:

function createService(dict, res) {
    // Ensure no duplicate
    var serviceExists = Service.find({name: dict['name']}).count().exec(function(err, doc) {
        return (doc !== 0);
    });

    console.log(serviceExists);

    if(serviceExists){
        res.send(500, "Duplicate document. Try updating existing entry or chooseing a different name");
        return;
    }

    //Insert the document
    service = new Service(dict);
    service.save(function(err) {
        if(!err) {
            res.send("Service saved");
        }
    });
}

The output of the console.log():

{ emitted: {},
  _events: { err: [Function], complete: [Function] } }

The end goal here is that the code will not reach the “Insert the document” portion if doc !== 0. Please let me know the correct way of doing this (Maybe using exceptions? That is the only idea I have left). Thanks

  • 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-09T05:17:53+00:00Added an answer on June 9, 2026 at 5:17 am

    Service.find is asynchronous. the callback in exec doesn’t execute immediately. This causes problem 1. (If Service....exec(...) returned a value, your console.log would have already excuted, before the callback.)

    Problem 2 is also pretty common. return in exec() doesn’t return a value you can assign to a variable. (exec() does not return the return value of your anonymous function.)

    Here is a fix for your code:

    function createService(dict, res) {
        // Ensure no duplicate
        Service.findOne({name: dict['name']}).count().exec(function(err, doc) {
            var serviceExists = (doc !== 0);
            console.log(serviceExists);
            if(serviceExists){
                res.send(500, "Duplicate document. Try updating existing entry or chooseing a different name");
                return;
            }
    
            //Insert the document
            service = new Service(dict);
            service.save(function(err) {
                if(!err) {
                    res.send("Service saved");
                }
            });
        });
    }
    

    I also changed find to findOne, otherwise you’ll get an array instead of a doc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been having trouble while attempting to use the nextLine() method from java.util.Scanner.
I'm attempting to break a list of words (a tokenized string) into each possible
I'm having issues with Silverlight 4 Out-Of-Browser, as specified in the title. What I
I am attempting to create a Backbone.js view based on a Twitter bootstrap-modal, which
I'm attempting to echo a PHP variable into my script, however it seems to
I'm having some difficulty attempting to create an SSL socket in Python to use
Ok, I need some help here. I'm attempting to write a program that rolls
I am attempting to print out lables (bar codes) from a table using JS
Attempting to build a C# NPAPI plugin I have found a tutorial which describes
Attempting to get Spring internationalization working. I have used classpath:messages basename, created .properties files

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.