2017-04-18 88 views
0

我定義了一個對象A,我需要存儲一些對象。 如何將查詢結果分配給對象的屬性。我如何分配查詢結果的sequelize對象的屬性

這是我的示例代碼。

function A(Job){ 
    this.job = Job; 
    this.location ={}; 
    models.locations.findOne({ 
     where:{ 
      job_id:this.job.id 
     } 
    }).then(function(location){ 
     //what should i do here to store it in A.locaiton 
    } 
} 

回答

0

持有thisthat屬性引用(或任何其他名稱你喜歡),然後使用that.location。例如:

function A(Job) { 
    var that = this; 
    that.job = Job; 
    that.location = {}; 
    models.locations.findOne({ 
     where: { 
      job_id: this.job.id 
     } 
    }).then(function (location) { 
     that.location = location; 
    }); 
} 

希望這有助於。