Rajesh Goriga
Rajesh Goriga

Reputation: 23

Meteor: How to Manipulate Mongodb data while displaying

In my Meteor app, I've successfully published data server-side and subscribed to that data client-side. Now, rather than pushing raw data straight to the client's screen, I want to perform some calculations on it, and render the result on the client's screen .

I can grab hold of Mongo data using the Template.example.helpers block and show that directly in the client as follows:

Template.example.helpers({
   order: function() {
     orders.find({})
  }
})

And it will be rendered to client side

        <thead>
            <tr>
              <th>Order ID</th>
              <th>Buyer Name</th>
              <th>Date</th>
              <th>Amount</th>
            </tr>
          </thead>
          <tbody>
            {{#each order}}

            <tr>
              <td>{{card_details.serialNo}}</td>
              <td>{{buyer_details.name}}</td>
              <td>{{card_details.time}}</td>
              <td>INR {{card_details.amount}}</td>
            </tr>
            {{/each}}
          </tbody>

Then i want to achieve to convert (card_details.amount)/100 and displayed the result to client side like <td>INR {{(card_details.amount)/100}}</td> Am I approaching this the right way? If so, how can I achieve it? Thanks!

Upvotes: 1

Views: 81

Answers (1)

Faysal Ahmed
Faysal Ahmed

Reputation: 1542

if you need changes some of the properties of a document(for example in your case, you just want changes in the card_details.amount without modifying the db), you can make another helper passing the original value to render the calculated value.

your blaze will be like this

{{dividedBy card_details.amount}}

and the helper will be like this

dividedBy: function(amount) {
    return amount/100 ;
}

Upvotes: 3

Related Questions