Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从Node.js中的Mongoose填充对象_Javascript_Oop_Node.js_Mongoose - Fatal编程技术网

Javascript 从Node.js中的Mongoose填充对象

Javascript 从Node.js中的Mongoose填充对象,javascript,oop,node.js,mongoose,Javascript,Oop,Node.js,Mongoose,如果没有数据库部分,您通常会在Javascript中创建一个新对象,如下所示: function object() { this.attribOne: 42, this.attribTwo: 'fourtytwo', [and so on creating more attribs and functions.] }; function object(data) { this.myProp1 = data.Prop1;//from db obj this.myPro

如果没有数据库部分,您通常会在Javascript中创建一个新对象,如下所示:

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

    [and so on creating more attribs and functions.]
};
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]

}
完成后,您将创建一个新的对象“实例”,如下所示

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,但是通过将从服务器返回的数据库对象传递到构造函数中,您可以非常轻松地完成您想要的任务:

可以按如下方式传递对象:

var myObject = new object(MongoDBObj);
然后在目标代码中,您可以执行如下操作:

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

    [and so on creating more attribs and functions.]
};
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]

}
编辑:我的第一条评论

你也可以这样做(简单化的例子)

更新3-显示以下讨论结果:

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]

}

我不确定我是否理解这个问题。从MongoDB查询中获得的文档已经是JavaScript对象了。是的,这是真的,但我想填充一个对象,该对象具有其他属性和函数,而这些属性和函数没有存储在数据库中。例如:this.attribPlus=this.attribOne+this.attribTwo;或类似的使用函数。。。我希望这是有道理的谢谢你的回复。我知道这个选项,但这意味着,我在对象的“外部”有一个DB调用。我希望将DB调用保留在对象内部,最好将其作为创建新对象时执行的函数。例如:var myObject=new Object().init([parameters]);其中init()函数执行DB调用并填充属性。想法相同。不要在create上传递选项,只需启动对象的方法并从数据库返回的值加载对象属性即可。您可以使用“this.property”从函数中引用对象的属性。我将在上面更新我的答案,请看这里的代码:(回调不必像那样传入。)我可以在DB调用中访问并设置“that”的值吗?啊,在调用“success”回调之前,在右边使用“that.prop”加载变量。我不熟悉MongoDB返回对象是什么?模态变量,或者传递给sucess处理程序的文档?我已经更新了fiddle,希望这能让您了解我在说什么。我假设文档是您从db获得的返回对象