简体   繁体   English

用猫鼬渲染额外的数据会导致车把

[英]Rendering extra data with Mongoose results in Handlebars

Say I have a function which finds all open bets in a collection: 假设我有一个函数可以找到一个集合中的所有未结投注:

app.get('/profile/bet-history', function(req, res, next){
        var user = req.user;
        if(user){
            Bet.find({$query : {"username" : user.username, "settled":false, "paired" : true}, $orderby : {_id : -1}})
                .then(function(doc){
                    res.render('profile/bet-history', {bets: doc, user: req.user}); 
            });
        }else{
            res.redirect('/login');
        }
    });

And I have a Handlebars template which renders the above bets and user data: 我有一个Handlebars模板,该模板呈现上述betsuser数据:

<tbody>
    {{#if bets}}
        {{#each bets}}
            <tr>
                <td>{{date this.createdAt}}</td>
                <td>{{this.market}}({{this.bet}})</td>
                <td>{{this.stake}}</td>
                <td>{{this.odds}}</td>
                <td>{{render the potential returns here}}</td>
            </tr>
        {{/each}}
    {{else}}
        <tr>
            <td>
                <p>You have no matched bets. Please come back later.</p>
            </td>
        </tr>
    {{/if}}        
</tbody>

I want to perform a quick "potential returns" calculation on each individual result returned from .find() by calling some function calcReturns() which takes in the returned bet and multiplies the stake by the odds and then rendering that result of the function with the data returned by the .find() call, which is rendered in an {{#each}} partial. 我想通过调用一些函数calcReturns()来对从.find()返回的每个结果进行快速的“潜在收益”计算,该函数接受返回的赌注并将赌注乘以赔率,然后使用.find()调用返回的数据,该数据以{{#each}}部分呈现。 A key requirement is I want to use a function in an external .js file and I don't want to have to use another field in the Mongo database. 一个关键要求是我想在外部.js文件中使用一个函数,并且我不想在Mongo数据库中使用另一个字段。

You could calculate the potential returns locally after the bets have been retrieved from the database. 从数据库检索到赌注之后,您可以在本地计算潜在的回报。

app.get('/profile/bet-history', function(req, res, next){
        var user = req.user;
        if(user){
            Bet.find({$query : {"username" : user.username, "settled":false, "paired" : true}, $orderby : {_id : -1}})
                .then(function(doc){
                    // Add the potential_returns for our render object here
                    doc.forEach(function(x) {
                        // Calculate the potential returns
                        x.potential_returns = x.bet * x.odds
                    })
                    res.render('profile/bet-history', {bets: doc, user: req.user}); 
            });
        }else{
            res.redirect('/login');
        }
    });

potential_returns can be add to the <tr> in the same way as the other properties of doc 可以像doc的其他属性一样,将potential_returns添加到<tr>

<tbody>
    {{#if bets}}
        {{#each bets}}
            <tr>
                <td>{{date this.createdAt}}</td>
                <td>{{this.market}}({{this.bet}})</td>
                <td>{{this.stake}}</td>
                <td>{{this.odds}}</td>
                <td>{{this.potential_returns}}</td>
            </tr>
        {{/each}}
    {{else}}
        <tr>
            <td>
                <p>You have no matched bets. Please come back later.</p>
            </td>
        </tr>
    {{/if}}        
</tbody>

Expanding on jervtub's answer, in order to use an external JS function, here's the final solution: 为了使用外部JS功能,请扩展jervtub的答案,这是最终的解决方案:

GET Request GET请求

var updates = require('./update.js');

app.get('/profile/bet-history', function(req, res, next){
        var user = req.user;
        if(user){
            Bet.find({$query : {"username" : user.username, "settled":false, "paired" : true}, $orderby : {_id : -1}})
                .then(function(doc){
                    // Add the potential_returns for our render object here
                    doc.forEach(function(x) {
                        updates.calcReturns(x);
                    })
                    res.render('profile/bet-history', {bets: doc, user: req.user}); 
            });
        }else{
            res.redirect('/login');
        }
});

Update.js File Update.js文件

module.exports = {
    calcReturns : function(x){
        if(x.bet == "Back"){
            x.potential_returns = x.stake * x.odds + x.stake;
        }else{
            x.potential_returns = x.stake * 2;
        }
    }
};

Handlebars Template 车把模板

<tbody>
    {{#if bets}}
        {{#each bets}}
            <tr>
                <td>{{date this.createdAt}}</td>
                <td>{{this.market}}({{this.bet}})</td>
                <td>{{this.stake}}</td>
                <td>{{this.odds}}</td>
                <td>{{this.potential_returns}}</td>
            </tr>
        {{/each}}
    {{else}}
        <tr>
            <td>
                <p>You have no matched bets. Please come back later.</p>
            </td>
        </tr>
    {{/if}}        
</tbody>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM