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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:22:52+00:00 2026-06-09T22:22:52+00:00

Without the database part you normally create a new object in Javascript like this:

  • 0

Without the database part you normally create a new object in Javascript like this:

function object() {
    this.attribOne: 42,
    this.attribTwo: 'fourtytwo',

    [and so on creating more attribs and functions.]
};

Once this is done, you create a new “instance” of the object like this

var myObject = new object;

And myObject will have the correct attributes, functions.

Is there a way to do this if I need to load the attribute values from MongoDB using Mongoose (asynchronously)?

Similar to this?!

function object() {
    /* list of attributes */
    this.attribOne: null,
    this.attribTwo: null,

    function init(){
       // mongoose db call
       // set attributes based on the values from db
    }
};

I looked at the init functions but it seemed that they don’t do what I need. (or I just didn’t get it)

I assume this is simple and I am overlooking the obvious, so please point me to the right direction. Thanks a lot!

  • 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-09T22:22:53+00:00Added an answer on June 9, 2026 at 10:22 pm

    I don’t know MongoDB, but you can pretty easily do what you want by passing in the database object you get back from the server into the constructor:

    You can pass the object in like so:

    var myObject = new object(MongoDBObj);
    

    Then in your object Code you could do something like this:

    function object(data) {
    
    this.myProp1 = data.Prop1;//from db obj
    this.myProp2 = data.Prop2;//from db obj
    
    this.myProp3 = getProp3Calculation(); //from global calculation
    
    [more functions and props for the object]
    
    }
    

    Edit: for my first comment

    You can do this as well (simplistic example);

    function object() {
    
    this.myProp1 = null;
    this.myProp2 = null;
    
    this.myProp3 = getProp3Calculation(); //from global calculation
    
    this.init = function([params]){
        var that = this;       
    
    
        var data = loadData(params);
    
        //if asynchronous the following code will go into your loadCompletedHandler
        //but be sure to reference "that" instead of "this" as "this" will have changed
        that.myProp1 = data.Prop1;
        that.myProp2 = data.Prop2;
    
    };
    
    [more functions and props for the object]
    
    }
    

    Update 3 – showing results of discussion below:

    function object() {
    
    this.myProp1 = null;
    this.myProp2 = null;
    
    this.myProp3 = getProp3Calculation(); //from global calculation
    
    this.init = function([params], callback){
        var that = this;       
    
    
    
        var model = [Mongoose Schema];
        model.findOne({name: value}, function (error, document) {
            if (!error && document){
    
                //if asynchronous the following code will go into your loadCompletedHandler
                //but be sure to reference "that" instead of "this" as "this" will have changed
                that.myProp1 = document.Prop1;
                that.myProp2 = document.Prop2;
    
                callback(document, 'Success');
    
    
            }
            else{
                callback(null, 'Error retrieving document from DB');
        }
        });
    
    
    
    };
    
    [more functions and props for the object]
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a way to insert new articles straight into my MediaWiki database without
In my Postgresql database, I have a timestamp without time zone with this value
Basically I need to connect to the database without any existing databases. In SQL,
I'm trying to store an image into an SQL database without using temporary files.
i want to get first n words from text stored in my database without
without using a database i wanted a file to point to the newest revision
I have a postgres database on my localhost I can access without a password
UPDATE: I had ghost entries in my database of games without players from before
I want to save Image in Database with JSON web service .without image data
How to securely store usernames in database, without adding them directly to cookie value?

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.