【发布时间】:2012-08-16 15:11:08
【问题描述】:
如果没有数据库部分,您通常会像这样在 Javascript 中创建一个新对象:
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 对象。
-
是的,没错,但我想填充一个对象,该对象具有未存储在数据库中的其他属性和功能。例如:this.attribPlus = this.attribOne + this.attribTwo;或类似的使用函数......我希望这是有道理的。 :)
标签: javascript oop node.js mongoose