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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:07:10+00:00 2026-06-10T18:07:10+00:00

I have the following code to insert data into DB – this code has

  • 0

I have the following code to insert data into DB – this code has to be executed in a sequential order

Router JS

module.exports = function(app) {

    app.get('/registerUser', function(req, res ) {

            objuser.userName  = 'testuser';
            objuser.password  = 'password';

            objuser.status  = true;

            registerUser (objuser ); //calls Business.js
            res.OK();
            res.end ();
    });
}

Business.js

var registerUser =  function (objuser  )
{


  userDB.registerUser (objuser ) ; //calls db.js

};

db.js

exports.registerUser  = function (objUser )
{

        var User = db.model(strCollectionName, UserSchema );

        var objSchema = new User(objUser);
        objSchema.save(function (err)
        {
            if (err)
               console.error (err);
            else
                console.log ("registerUser : Data insertion success.");

        });

}

In the db.js Im getting error from Mongo if I try to insert duplicate value. I wan to pass the error message to HTML page to display the same. What should I do? I tried

throw Error (err)

But it breaks the server.

  • 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-10T18:07:12+00:00Added an answer on June 10, 2026 at 6:07 pm

    Assuming you are using expressjs, I’d make use of the next callback. like so:

    app.get('/registerUser', function (req, res, next) {
        objuser.userName  = 'testuser';
        objuser.password  = 'password';
    
        objuser.status  = true;
    
        registerUser(objuser, function (err) {
            if(err) {
                //this will be handled by express's errorHandler or whatever you have configured
                return next(err); 
            }
            //do whatever you want to do with the response
        });
    });
    

    If you don’t want to make your Business.js call async then you will obviously change this code to a try...catch flow. Node.js apps are happier using async calls though, so a common convention in nodejs apps is to expose a callback using the (err, result) parameters. So your db.js call would be :

    exports.registerUser  = function (objUser, callback )
    {    
            var User = db.model(strCollectionName, UserSchema );
            var objSchema = new User(objUser);
            objSchema.save(function (err) {
                if (err) return callback(err);
                return callback(null, objSchema);
    
            });
    }
    

    By now you probably notice that your Business.js call would just be a mediator between your route and your db code…whether you need it or not is up to you.

    HTH,
    Mike

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

Sidebar

Related Questions

I have following code to insert data into GiftCouponPayment table and Payment table. This
I have the following code I use to insert form data into a single
I have the following code that creates my table. I insert data into it
i have the following code: <!--ajouter une carte --> <?php if($_POST[submit_dd]){ mysql_query(INSERT INTO data
I have the following python code which pushes data to db: cursor.execute(INSERT INTO +
I have following code for inserting data into database using PDO. It inserts data
I have table that I insert data with following query (from c# code): INSERT
I have the following code: def add_record(self,values): self.sql.execute(INSERT INTO TEST VALUES (?,?) % values)
I have the following code: <? $query =$db->prepare(INSERT INTO a_table (id, a_field) VALUES ('',
I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT

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.