2016-11-16 4 views
0

私はjavascriptとノードを使い慣れていないため、mongooseクエリの結果を私が扱えるオブジェクトに返すのに問題があります。私のアプリは現在、キャッシュへのポストリクエストのボディを解析し、キャッシュされたオブジェクト(mlcRecord.existFixture)のフィールドを使用してデータベースにクエリを行い、追加のプロパティを取得するオブジェクトを返します。コードのこの部分は正常に動作します。ただし、追加のプロパティは、スコープの外部では定義されていません。親スコープで利用できないmongooseクエリの結果

私は何か基本的なものが欠けていると確信していますので、指導者が提供することができます。

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); 
    }); 
}); 
+0

はあなたの質問からファイルの全体に置くことができますか? – num8er

答えて

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); 
     }); 
    }); 

    }); 
+0

ありがとう@ num8er。あなたのソリューションをテストした後、私はそれが私の目的のためにうまくいかないことが分かった。私はmlcRecordに基づいてデータパイプラインを構築しているので、実際にはasync.waterfallが必要です。私が持っていた問題は、実行コンテキストに関連していたことが判明しました。私はこの問題に関心のある人のために以下のコメントとして私の解決策を入れました。 – tpick

0

私はこの問題は、実行コンテキストとあった正確に何がわからないんだけど、それはと思われますltgTechnologies.findOneによって返された約束の中のmlcRecordは、約束の範囲外ではアクセスできませんでした。 setWattages関数で約束を直ちに返すと、結果として得られる約束を束縛して、私の問題を解決しました。私はまた、いくつかのフィールドの名前を変更し、モジュールとして機能するように移動しました。以下のコード。

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 
]}; 
} 
関連する問題