简体   繁体   中英

How to show a simple value from a meteor collection in a meteor template?

I want to display an object from a collection in meteor using handlebars.

I have this example about what I have:

Collection: Transactions.

Server side:

Meteor.publish('getTransaction', function(transactionid){
    return Transactions.findOne({transactionid:transactionid});
});

Client side:

HTML

    <template name='MainTransac'>

    <body>
      <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title" align="center">General information</h3>
        </div>
        <div class="panel-body">
                  <table class="table table-striped">
                    <tbody>
                       <tr>
                        <td>TransactionID:</td>
                       </tr>
                       <tr>
                        <td>{{transactionid}}</td>
                       </tr>
                    </tbody>    
                  </table>
        </div>
      </div>   
    </body>
   </template>

JS

Template.MainTransac.onCreated(function(){   
this.subscribe('getTransaction',Router.current().params.transactionid); 
});

As you can see I have receiving the trasactionid from another page. But I am not able to know how can I show the table with the transaction id or any other field from the transaction document.

I really appreciate all your help about it.

If you have any question just let me know, have a great day.

As first you have to create a helper which will contains your collection data:

Template.MainTransac.helpers({
  transaction: function(){ return Transactions.find(); }
});

Then you will be able to include that helper in your template:

<tbody>
<tr>
<td>TransactionID:</td>
</tr>
<tr>
{{#with transaction}}
  <td>{{transactionid}}</td>
{{/with}}
</tr>
</tbody>

Hope it works !

A publication must return a cursor or an array of cursors . You have:

Meteor.publish('getTransaction', function(transactionid){
    return Transactions.findOne({transactionid:transactionid});
});

which is returning an object.

Just change the .findOne() to .find()

Meteor.publish('getTransaction', function(transactionid){
    return Transactions.find({transactionid:transactionid});
});

Then your publish() will work as will your subscribe() . You still need to select the right data context for your template via a template helper.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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