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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:17:09+00:00 2026-06-01T20:17:09+00:00

I’m attempting to model the data in my database as classes in server-side JavaScript

  • 0

I’m attempting to model the data in my database as “classes” in server-side JavaScript (Node).
For the moment, I’m simply using the traditional constructor pattern with prototype methods where appropriate and exposing the constructor function as the entire module.exports. The fact that this is server side is unimportant to the core of the question, but I figured I’d provide it as reference.

The problem area of code looks like this:

User.prototype.populate = function() {  
    var that = this;    
    db.collection("Users").findOne({email : that.email}, function(err, doc){
        if(!err) {
            that.firstName  = doc.firstName;
            that.lastName   = doc.lastName;
            that.password   = doc.password;
        }
        console.log(that); //expected result
    });
   console.log(that); //maintains initial values
};

Whenever I call this function, changes to the object do not persist once the findOne() has completed. I realize that the scope of this changes to the global object with new function scopes, so I maintained its reference as that. If console.log(that) from within the anonymous function, the data shows up in the properties of it as expected. However, if I log that once the function has finished, it maintains the state it had at the beginning of the function.

What’s going on here and how I can I change instance variables as expected?

  • 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-01T20:17:09+00:00Added an answer on June 1, 2026 at 8:17 pm

    “However, if I log that once the function has finished, …”

    By this I assume you’re doing something like this…

    var user = new User
    
    user.populate();
    
    console.log(user);
    

    If so, the console.log will run long before the asynchronous callback to .findOne() has been invoked.

    Any code that relies on the response to findOne needs to be invoked inside the callback.


    EDIT: Your update is a little different from my example above, but the reason is the same.

    The entire reason for passing a callback to the findOne method is that it performs an asynchronous activity. If it didn’t, there’s be no reason for the callback. You’d just place the inner code directly after the call to findOne, as you did with the console.log().

    But because it’s asynchronous, the subsequent code doesn’t wait to execute. That’s why you’re getting the unpopulated object in the console.

    If you add a label to each console.log(), you’ll see that they execute out of order.


    var that = this;    
    db.collection("Users").findOne({email : that.email}, function(err, doc){
        if(!err) {
            that.firstName  = doc.firstName;
            that.lastName   = doc.lastName;
            that.password   = doc.password;
        }
        console.log("inside the callback", that); // this happens Last!!!
    });
    
    console.log("outside the callback", that); // this happens First!!!
    

    So it becomes clear once you observer the order of the console.log calls that the empty one is happening before the one inside the callback.


    EDIT: You can also have your .populate() method receive a callback that is invoked inside the .findOne callback.

    User.prototype.createNickName = function () {
        this.nickname = this.firstName.slice(0,3) + '_' + this.lastName.slice(0,3);
    };
    
        // >>>------------------------------v----receive a function argument...
    User.prototype.populate = function(callback_func) {  
        var that = this;    
        db.collection("Users").findOne({email : that.email}, function(err, doc){
            if(!err) {
                that.firstName  = doc.firstName;
                that.lastName   = doc.lastName;
                that.password   = doc.password;
            }
    
              // all users will have the "createNickName()" method invoked
            that.createNickName();
    
              // ...and invoke it in the callback.
            callback_func.call(that);
    
              // By using .call(), I'm setting the "this" value
              //    of callback_func to whatever is passed as the first argument
        });
    };
    

       // this user wants to log the firstName property in all caps
    var user1 = new User;
    
    user1.populate(function() {
        console.log(this.firstName.toUpperCase());
    });
    
    
       // this user wants to log the the whole name
    var user2 = new User;
    
    user2.populate(function() {
        console.log(this.firstName + ' ' + this.lastName);
    });
    
    
       // this user wants to verify that the password is sufficiently secure
    var user3 = new User;
    
    user3.populate(function() {
        console.log(verify_password(this.password));
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.