【问题标题】:Populating Object from Mongoose in Node.js在 Node.js 中从 Mongoose 填充对象
【发布时间】: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


【解决方案1】:

我不了解 MongoDB,但是您可以通过将从服务器返回的数据库对象传递给构造函数来轻松地做您想做的事情:

你可以像这样传递对象:

var myObject = new object(MongoDBObj);

然后在您的对象代码中,您可以执行以下操作:

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]

}

编辑:我的第一条评论

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

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]

}

更新 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]

}

【讨论】:

  • 感谢您的回复。我知道这个选项,但这意味着,我在我的对象“外部”有一个数据库调用。我想将 DB 调用保留在对象内部,最好作为创建新对象时执行的函数。例如: var myObject = new Object().init([parameters]);其中 init() 函数执行 DB 调用并填充属性。
  • 同样的想法。无需在创建时传入选项,只需触发对象的方法并从从数据库返回的值中加载对象属性。您可以使用“this”.property 从函数中引用对象的属性。我会在上面更新我的答案
  • 请在此处查看代码:pastebin.com/JjtJxrQG(不必像那样传入回调。)我可以在 DB 调用中访问和设置“that”的值吗?
  • 啊,在你调用“成功”回调之前,使用“that.prop”加载变量。我对 MongoDB 不熟悉,返回对象是什么?模态变量,还是传递给成功处理程序的文档?
  • 我已经更新了小提琴,希望这能让你了解我在说什么。我假设该文档是您从数据库获得的返回对象
猜你喜欢
  • 2016-09-03
  • 2022-01-24
  • 2016-01-19
  • 2018-08-11
  • 2022-08-02
  • 2018-09-10
  • 2016-10-12
  • 2017-10-26
  • 1970-01-01
相关资源
最近更新 更多