【问题标题】:Meteor, cannot retrieve document data from a collection to the client within a templateMeteor,无法在模板中从集合中检索文档数据到客户端
【发布时间】:2015-02-10 01:07:28
【问题描述】:

我在 Meteor 中向客户端显示数据库中的项目时遇到了困难。在模板 user_places 中,我想显示集合中文档的内容,并根据文档的创建对每个位置进行排序。

但是,尽管代码编译并运行,但我看不到 {{>user_places{{ 所在的位置。

我怀疑我输入了错误的查询:

return Places_Collection.find({ownerId: Meteor.userId()}, {sort: {添加时间:-1}});

无论如何,这是我的代码。注意 Meteor.userId() 确实包含一个值,所以让我们假设有一个用户登录。

html:

<head>
<title>Test</title>
<head>

<body>
{{ul}}    
{{#each places}}
{{>user_places}}
{{/each}}
{{/ul}}    
...
</body>

<template name="user_places">
<li>You have been to {{place_id}} from {{start}} to {{end}}</li>
</template>

javascript:

...
//client code
Meteor.subscribe('places_collection');

Template.user_places.helpers({

 places: function() {
return Places_Collection.find({ownerId: Meteor.userId()}, {sort: {addedAt: -1}});
}
)};
 ...

Places_Collection (Mongodb) 中的示例文档

{
added_At: ISODate("2015-0207TIF:39:48:837Z"), 
ownerId: "~userToken~",
start: "2014-02-02",
end: "2014-04-03:,
place_id "Canada"
_id: "~documentToken~"
}

如果您需要更多代码,请随时提出。

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    尝试在 Template.body 中定义 places 助手,因为它在 &lt;body&gt;&lt;/body&gt; 中而不是模板定义中。

    Template.body.helpers({
      places: function() {
        return Places_Collection.find({ownerId: 'asdf'}, {sort: {addedAt: -1}});
      }
    });
    

    这是一个工作版本: http://meteorpad.com/pad/uk8LzZPGs4nmjuhQ6/Example%201

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 2014-01-16
      • 1970-01-01
      相关资源
      最近更新 更多