来自父范围的mongoose查询的结果

时间:2016-11-16 02:15:44

标签: node.js mongodb mongoose

我是javascript和节点的新手,我很难将结果从mongoose查询返回到我可以使用的对象。我的应用程序当前将后期请求的主体解析为缓存,使用缓存对象中的字段(mlcRecord.existFixture)来查询数据库,并返回一个对象,从中获取其他属性。这部分代码工作正常。但是,在.then范围之外的其他属性是未定义的。

我确信我遗漏了一些基本内容,所以任何指导人员都可以提供。

router.route('/mlc')
.post(function (req,res){
   var mlcRecord = new mlcInputObj(req.body);
   async.series([
      function (callback) {
        function setWattages(mlcRecord) {

        // load the existFixture TechnologyID
        ltgTechnologies.findOne({TechnologyID: mlcRecord.existFixture}).exec()

        // capture the technologyID 
            .then(function(ltgTechnology){
                mlcRecord.existFixtureWatts = ltgTechnology.SystemWatts;
                return mlcRecord;
            });
        }

        setWattages(mlcRecord);

        console.log('mlcRecord: ', mlcRecord);  // existFixtureWatts displays as undefined
        callback();
     }
   ], function (err) {
    res.json(mlcRecord);
   });
});

2 个答案:

答案 0 :(得分:1)

您的代码过于复杂。

async.series不适合您的目的。

以下是修复:

router
  .route('/mlc')
  .post(function (req,res){
    var mlcRecord = new mlcInputObj(req.body);

    // load the existFixture TechnologyID
    ltgTechnologies
      .findOne({TechnologyID: mlcRecord.existFixture})
      .exec(function(err, result) {
        mlcRecord.existFixtureWatts = null;
        if(result) {
          mlcRecord.existFixtureWatts = result.SystemWatts;
        }
        res.send(mlcRecord);
      });
  });

但是如果要将mlcRecord保存到数据库:

router
  .route('/mlc')
  .post(function (req,res){

    var mlcRecord = new mlcInputObj(req.body); // creating mlcRecord instance

    mlcRecord.save(function(err) { // inserting to database
      if(err) return res.status(500).send({err});

      // adding to mlcRecord  existFixtureWatts
      ltgTechnologies
        .findOne({TechnologyID: mlcRecord.existFixture})
        .exec(function(err, result) {
          mlcRecord.existFixtureWatts = null;
          if(result) {
            mlcRecord.existFixtureWatts = result.SystemWatts;
          }
          res.send(mlcRecord);
        });
    });

  });

答案 1 :(得分:0)

我不确定执行上下文究竟是什么问题,但似乎在承诺之外无法访问ltgTechnologies.findOne返回的承诺中的mlcRecord。在setWattages函数中立即返回promise,然后绑定生成的promise,解决了我的问题。我还重命名了几个字段并转移到一个模块。代码如下。

app.js

// some code ...
router
.route('/mlc')
.post(function (req,res){

var mlcRecord = new mlcInputObj(req.body);

async.waterfall([

    function (callback) {
        mlcRecord.setOccSensorScenario();
        mlcRecord.setltgTechnologyVals().then(function(){
            callback(null);
        });
    }, 
    // more functions to execute sequentially

], function (err, result) {
    res.json(mlcRecord);
});
});
// some other code ...

mlcInputObj.js(构造函数)

// constructor
// ... 
// constructor methods

mlcInputObj.prototype.setltgTechnologyVals = function () {
//alias for 'this' used in bind
var mlcObj = this;

// load the existFixture TechnologyID
return Promise.all([
    // query ltgTechnologies and set existFixtureWatts
    ltgTechnologies.findOne({TechnologyID: this.existFixture}).exec()
    .then((function(fixture){
        this.existFixtureWatts = fixture.SystemWatts;
    }).bind(mlcObj)),
    // another promise,
    // last promise
]};
}