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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:47:58+00:00 2026-06-10T08:47:58+00:00

Lets take a look at the following code: mongodb.js var mongo = require(‘mongodb’) ,

  • 0

Lets take a look at the following code:

mongodb.js

var mongo = require('mongodb')
  , mongodb = module.exports;

mongodb.database = function(key, dbName, dbServer, callback){
  var db = new mongo.Db(dbName, dbServer);
  db.open(function(error, connection){ // 5
    if(error){ // 1
      throw error;
    }

    return callback(null, connection);
  });
};

mongodb.collection = function(dbKey, collName, callback){
  mongodb.database(dbKey, function(error, connection){
    connection.collection(collName, function(error, collection){ // 2
      if(error){ // 3
        throw error;
      }

      return callback(null, collection);
    });
  });
}

model.js

var db = require('./mongodb.js');
var model = module.exports;

model.test = function(callback){
  db.collection('users', function(error, collection){ // 4
    // do something.
  });
}

My question is, why do we always do “error, resource” when a callback is passed through, because, take a look at 1

We are dealing with the error, so it can’t even reach 2*

Same with 3 and 4 they will always be NULL, because the error is handled on a different layer. Now why wouldn’t I just do this at 5:

db.open(function(error, connection){
  if(error){ // 1
    return errorHandler(error);
  }

  return callback(collection);
});

the errorHandler function would be a function that deals with major errors, so we don’t have to use them at each layer, same story for for example mongodb.findOne, why do we have to deal with errors when we pass in the callback?

  • 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-10T08:48:00+00:00Added an answer on June 10, 2026 at 8:48 am

    I use the callback(error,result) pattern mostly when the error is not thrown anywhere (or catched). I.e. you dont want to stop your application just because of some missing parameter. Look at the following code:

    function somethingAsync (options, callback) {
      if (!options || !options.name) {
        callback('no name specified'); //we dont want to kill the Application
        return;
      };
      doSomeThingInternal(callback);
    };
    

    On the consumer side you can print the error to the client from any internal function.

    somethingAsync(null, function (err,res)) {
      if(err) {
        console.log('error occured: ' + err);
      };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So lets take a look onto a littel bit modified example code: #include <stdio.h>
Let's first take a look at JavaScript code => function ConstructNodes(className,hiddenId,alertMsg,formName){ this.className = className;
Take a look at the first example of http://code.google.com/intl/sv-SE/appengine/docs/java/datastore/jdo/relationships.html (Owned One-to-One Relationships). I dont
Take a look at this awesome code from John P Bloch. What this code
Lets take a look to this two functions: std::string get_string() { std::string ret_value; //
Take a look at this code: $cache = Zend_Cache::factory('Output', 'File', $frontendOptions, $backendOptions); // we
EDIT To simplify this further. Let's take a look at this structure: Table1: idProduct
Lets take stackoverflow as an example: Post Postid Title Mess -------------------------------------------------- 1 Title1 This
I have a very basic question. Lets take this snippet: #include <stdio.h> void foo(void)
Is it possible to generate comments for closing div tags, lets take this ex.

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.