Meteor无法从模板中的集合中检索文档数据到客户端

时间:2015-02-07 21:13:45

标签: mongodb meteor

我很难在Meteor中将数据库中的项目显示给客户端。在模板 user_places 中,我想显示集合中文档的内容,并通过创建文档对每个位置进行排序。

然而,虽然代码编译并运行但我看不到{{> user_places {{所在位置。

我怀疑我将错误的查询放入:

  

返回Places_Collection.find({ownerId:Meteor.userId()},{sort:   {addedAt:-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~"
}

如果您需要更多代码,请不要犹豫。

1 个答案:

答案 0 :(得分:1)

尝试在places中定义Template.body帮助器,因为它在<body></body>内,而不是模板定义。

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

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

相关问题