如何在模型的属性方法中正确使用水线承诺?
在Model
我使用的属性方法将查找数据库,不得不查询其他不同的模型。
所以我使用水线承诺,但我不知道如何实际执行模型的属性方法本身的返回方法,这就是我正在做的:
我们称之为LineUp模型:
module.exports = { connection: 'seasonDB', attributes: { reachesMaxFavoriteTeam: function (team) { UserTeam.findOne(). where({id: this.userTeam}). then(function (team) { User.findOne(). where({id: team.userId}). then(function (user) { return [team, user.fanOf]; // I assume to be returning for the spread function }). spread(function (team, user) { // How can I make reachesMaxFavoriteTeam return something here??? }).catch(function (err) { }); }); } };
所以我不知道如何甚至在哪里我应该为LineUp
本身的reachesMaxFavoriteTeam
属性方法执行返回。
您返回整个承诺链:
reachesMaxFavoriteTeam: function (team) { return UserTeam.findOne(). where({id: this.userTeam}). then(function (team) { User.findOne(). where({id: team.userId}). then(function (user) { return [team, user.fanOf]; }). spread(function (team, user) { return THE_RESULT; }).catch(function (err) { }); }); }
您的通话代码:
lineUpInstance.reachesMaxFavoriteTeam(team).then(function(result) { // result === THE_RESULT here });