我定义了一个对象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
}
}
答案 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;
});
}
希望这有帮助。