如何在流星模板中显示流星集合中的简单值?

时间:2022-09-06 18:50:13

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.

正如您所看到的,我从另一个页面收到了trasactionid。但我无法知道如何使用交易ID或交易文档中的任何其他字段显示该表。

I really appreciate all your help about it.

我真的很感谢你的帮助。

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

如果您有任何疑问,请告诉我,祝您度过愉快的一天。

2 个解决方案

#1


0  

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()

只需将.findOne()更改为.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.

然后你的publish()将像你的subscribe()一样工作。您仍然需要通过模板助手为模板选择正确的数据上下文。

#2


0  

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 !

希望它有效!

#1


0  

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()

只需将.findOne()更改为.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.

然后你的publish()将像你的subscribe()一样工作。您仍然需要通过模板助手为模板选择正确的数据上下文。

#2


0  

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 !

希望它有效!