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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:22:46+00:00 2026-05-31T15:22:46+00:00

For organization purposes I’m separating my source code into modules, for example I have

  • 0

For organization purposes I’m separating my source code into modules, for example I have the user module on my node.js app which is responsable for retrieving user information from a MongoDB database. I’m doing something like this:

var mongo = require("mongodb"),
    Server = mongo.Server,
    Db = mongo.Db;

var server = new Server("localhost", 27017, { auto_reconnect: true });
var db = new Db("users", server);

module.exports = {
    login: function(user, pass, callback) {
        var reg_result = null;

        db.open(function (err, db) {
            if(!err) {
                db.collection("users", function(err, collection) {
                    collection.findOne(
                        {
                            "username": user,
                            "password": pass
                        },
                        function(err, item) {
                            if(!err) {
                                reg_result = item;
                            } else {
                                reg_result = "error";
                            }
                        }
                    );
                });
            } else {
                reg_result = "error";
                console.log("ERROR: " + err);
            }
        });

        callback(reg_result);
    }
}

And executing it on my test script like this:

var user = require("./user.js");

user.log("test", "test", function(msg) {
    console.log(msg);
});

It does the database operation and retrieves the value, but every time it only returns null, when I don’t initialize the reg_result variable it returns undefined. What should I do to correct this?

I debugged using console.log on the user.js and the item was outputted, but I want to have the callback so I can use the item on other sources, like my test script

  • 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-05-31T15:22:47+00:00Added an answer on May 31, 2026 at 3:22 pm

    You are calling the callback function right away, but going to the db takes time and is therefore done asynchronously. Instead, call your callback at the appropriate time using the result argument of the function passed as the last parameter to the .findOne() function. The callback should get an error argument and a result argument:

    login: function(user, pass, callback) {
        db.open(function (err, db) {
            if(!err) {
                db.collection("users", function(err, collection) {
                    collection.findOne(
                        {
                            "username": user,
                            "password": pass
                        },
                        function(err, item) {
                            if(!err) {
                                callback(null,item);
                            } else {
                                callback("error");
                            }
                        }
                    );
                });
            } else {
                callback("error",null);
            }
        });
    }
    
    
    user.login("test", "test", function(err,msg) {
        if( err ) {
          //error occured above;
        } else {
          //success
          console.log(msg);
        }
    });
    

    this is just pulling the same pattern through that the mongodb-driver is using.
    hope it helps.

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

Sidebar

Related Questions

Problem: In our organization we have a home grown single-sign-on app written in c#/.Net2
The organization I currently work for an organization that is moving into the whole
My organization is getting ready to implement a new system, which is a asp.net
Our organization's software is compiled for the .NET 3.5 Framework. We have some customers
I have written a project for my students organization. I would like to share
I have a XML input which is obtained dynamically.How can i write the style
I have an Eclipse workspace that is checked into a Subversion repository. However, if
After thinking for long, I have decided to build my data app for the
I have a pretty basic positional inverted index, in which I store a lot
I'm currently developing a monthly checklist system for our organization. A user may login,

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.