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!
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:
Then in your object Code you could do something like this:
Edit: for my first comment
You can do this as well (simplistic example);
Update 3 – showing results of discussion below: