在模型
中,我使用属性方法来查找数据库,必须查询其他不同的模型。
所以我使用水线 promise ,但我不确定如何实际执行模型属性方法本身的返回方法,这就是我正在做的:
我们将其称为 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
属性方法的返回。
最佳答案
您返回整个 promise 链:
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
});
关于node.js - 如何在模型的属性方法中正确使用水线 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30785622/