流星没有加载数据

时间:2016-04-06 01:12:28

标签: meteor

我正在调用我的模板{{> list_items}},因某些原因它无法正常工作

这是模板代码

<template name="list_items">
  {{#each items}}
    <div class="col-sm-4 col-lg-4 col-md-4">
      <div class="thumbnail">
        <img src="{{previewImage}}" alt="">
        <div class="caption">
          <h4 class="pull-right">{{price}}</h4>
          <h4><a href="#">{{title}}</a></h4>
          <p>{{description}}</p>
        </div>
      </div>
    </div>
  {{/each}}
</template>

items是模板助手中的一个函数,它返回我的集合中的文档,这里是它的代码:

Template.list_items.helpers({
  items: function(){
    return Items.find({});
  }
});

这是我的项目集合允许规则

Items.allow({
  insert: function (userId, doc) {
    // the user must be logged in, and the document must be owned by the user
    return true;
  },
  update: function (userId, doc, fields, modifier) {
    // can only change your own documents
    return true;
  },
  remove: function (userId, doc) {
    // can only remove your own documents
    return true;
  }
});

收集内部&#34; lib&#34;文件夹,以便我可以从客户端使用它。

当我尝试使用Items.find()。fetch()时,我得到一个空集合[]即使我有一个包含一个文档的集合

为什么它不起作用?是否有必要首先添加的包?

2 个答案:

答案 0 :(得分:1)

通过发布和订阅来解决它

在服务器

Meteor.publish('items-all', function publishFunction() {
  return Items.find({});
})

在客户端

Meteor.subscribe('items-all');

答案 1 :(得分:0)

这不允许您在客户端找到任何数据.Items.find({})将返回空文档。

Items.allow({
  insert: function (userId, doc) {
    // the user must be logged in, and the document must be owned by the user
    return true;
  },
  update: function (userId, doc, fields, modifier) {
    // can only change your own documents
    return true;
  },
  remove: function (userId, doc) {
    // can only remove your own documents
    return true;
  }
});

如果要允许Items.find({})允许所有路由/模板。您可以在服务器端发布数据,如:

Meteor.publish('all-Items', function(){
  return Items.find({});
})

在客户端,您可以在Meteor.startup内订阅all-Items发布,如下所示:

Meteor.startup(function () {
    Meteor.subscribe('all-Items');
}

然后你可以在每一条路线上访问Items.find({}),并且会反复返回所有文档。

不要忘记添加fetch函数以在控制台上查看:Items.find({})。fetch(),它将返回文档的数组。