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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:26:50+00:00 2026-06-18T20:26:50+00:00

Consider following codes: mongoose.connect(‘MyDatabaseURL’); var sch_obj = {field1: String}; var db = mongoose.connection; db.on(‘error’,

  • 0

Consider following codes:

mongoose.connect('MyDatabaseURL');
var sch_obj = {field1: String};
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
    var model_obj = db.model('SchemaName', sch_obj);
    var obj = new model_obj({field1:'MyValue'});
    obj.save(function(err,data){
      if(err) 
         console.log('error occurred:' + err);   // <=== Case 1
      else
         console.log('saved'); 
      });
});

/* ----------------------------------  */

mongoose.connect('MyDatabaseURL');
var sch_obj = {field1: String};
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
    var model_obj = db.model('SchemaName', sch_obj);
    var obj = new model_obj({field1:'MyValue'});
    obj.save(function(err,data){
      try {
          console.log('saved');
      }
      catch(err)                            // <=== Case 2
      {
        console.log('error occurred:' + err);
      }
    });
});

Question: Are they same? If yes which one is the good way to handle error?

  • 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-18T20:26:51+00:00Added an answer on June 18, 2026 at 8:26 pm

    No, they are not the same. Mongoose newer throws errors, so you’ll newer catch one. It’s why you can’t use try ... catch ... statement to handle mongoose errors.

    So, the only right way is:

    obj.save(function(err,data){
      if(err)
         console.log('error occurred:' + err);
      else
         // err == null
         console.log('saved'); 
      });
    

    The reason behind it is that throw terminates the current process unless it caught. So, most nodejs modules (e.g. express, mongoose, etc.) passes their errors using callback instead of throwing them, its a common practice in nodejs.

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

Sidebar

Related Questions

Consider the following sample codes: 1.Sample var IsAdminUser = (User.Privileges == AdminPrivileges) ? 'yes'
For example lets consider the following 2 codes: for (i = 0; i <
Consider the following: var foo = [] for (var i=0; i<100000; i++) { foo.push(97);
Background To replace invalid zip codes. Sample Data Consider the following data set: Typo
Consider following codes: #include <stdio.h> #include <malloc.h> void allocateMatrix(int **m, int l, int c)
Consider the following C++ codes : using namespace std; vector<char*> aCharPointerRow; aCharPointerRow.push_back(String_11); aCharPointerRow.push_back(String_12); aCharPointerRow.push_back(String_13);
Consider the following code block: using (PlayersDataContext context = new PlayersDataContext()) { Console.WriteLine(context.Players.Count()); //
consider the following codes: class MyJob { def execute() { println Hello at->+new Date()
Consider following code: ArrayList<Integer> aList = new ArrayList<Integer>(); aList.add(2134); aList.add(3423); aList.add(4234); aList.add(343); String tmpString
Consider following code public class City { public string Name { get { return

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.