从猫鼬填充对象,而数据库的一部分,你通常在Javascript中创建一个新的对象是这样的:在Node.js的
function object() {
this.attribOne: 42,
this.attribTwo: 'fourtytwo',
[and so on creating more attribs and functions.]
};
一旦做到这一点,你像这样创建的对象的新“实例”
var myObject = new object;
而myObject将具有正确的属性和功能。
如果我需要使用Mongoose(异步)从MongoDB加载属性值,有没有办法做到这一点?
与此相似?
function object() {
/* list of attributes */
this.attribOne: null,
this.attribTwo: null,
function init(){
// mongoose db call
// set attributes based on the values from db
}
};
我看着init函数,但似乎他们没有做我所需要的。 (或我只是没有得到它)
我认为这很简单,我忽略了明显的,所以请指向正确的方向。非常感谢!
我不知道我理解的问题。您从MongoDB查询中获得的文档已经是JavaScript对象。 – JohnnyHK 2012-08-16 15:18:04
是的,这是真的,但我想填充一个对象,其中有没有存储在数据库中的其他属性和功能。例如:this.attribPlus = this.attribOne + this.attribTwo;或使用功能类似...我希望这是有道理的。 :) – 2012-08-16 15:22:28