如何将sequelize的查询结果分配给对象的属性

时间:2017-04-18 14:40:49

标签: node.js sequelize.js

我定义了一个对象A,我需要在其中存储一些对象。 如何将查询结果分配给对象的属性。

这是我的示例代码。

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

1 个答案:

答案 0 :(得分:0)

this的引用保存到that属性(或您喜欢的任何其他名称)中,然后使用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;
    });
}

希望这有帮助。