如何在ejs中包含promise值?

我在expressejs帮助下制作一个小型的节点应用程序。 在我的一个控制器中,我返回一个对象,该对象具有我希望在我看来包含的承诺属性。 我试图找出什么是最好的办法做到这一点,因为它似乎ejs不能处理承诺的价值观。 我想在模型中包括承诺的价值,但似乎我过分复杂的代码需要完成。

如何在ejs轻松呈现bluebird承诺的价值?

调节器

 compaintsRouter.get('/', function(req, res) { var model = data.complaint.findAndCount({ include: [data.user, { model: data.comment, include: [data.user] }], limit: 15, offset: 15 * (req.query.page || 0) }).then(function(result){ res.render('complaints/list', {model: result.rows}); }); }); 

ejs查看模板

 <% model.forEach(function(complaint) { %> <div class="complaint"> ... more html here... <footer> <span class="vote brand-success"> <% complaint.votesUp().then(function(votes){%> <%= votes %> <%}) %> <span class="glyphicon glyphicon-thumbs-up clickable"></span> </span> </footer> ... more html here... </div> <% }) %> 

承诺方法:

 votesUp: function() { return this.getFollower().then(function(followers){ var aux = followers .selectMany(function(f){return f.UserComplaint}) .map(function(v){ return ((v.liked === true) ? 1: 0); }); aux.push(0); aux.reduce(function(acc, v){ return acc+v; }); }) } 

似乎答案可能是将业务逻辑混合到视图中。 你能否早些得到选票,这样你就不必在模板中混淆了? 看起来很凌乱。 🙂

(ps,+1来抵消downvote)

似乎我错过了我的orm的function,这是续集。 只要我在查询中包含模型,它们就允许我检索关联。 需要做的修改如下:

调节器

 compaintsRouter.get('/', function(req, res) { var model = data.complaint.findAndCount({ include: [data.user, { model: data.comment, include: [data.user] }, { model: data.user, as: 'follower' }], limit: 15, offset: 15 * (req.query.page || 0) }).then(function(result){ res.render('complaints/list', {model: result.rows}); }); }); 

使用数据的方法:

 votesUp: function() { var aux = this.follower .selectMany(function(f){return f.UserComplaint}) .map(function(v){ return ((v.liked === true) ? 1: 0); }); aux.push(0); return aux.reduce(function(acc, v){ return acc+v; }); }