【问题标题】:Meteor publish subscribe wrong output流星发布订阅错误输出
【发布时间】:2015-06-25 04:55:45
【问题描述】:

我是 Nodejs / Meteor 和 Mongodb 的新手。我遇到了这个问题,当我尝试在流星中使用发布和订阅时我无法获取我的数据

lib/collection.js

Reports = new Meteor.Collection('reports'); 

lib/routes.js

Router.map(function() {
  this.route('dashboard', {
    path: '/dashboard',
    waitOn: function () {
      return Meteor.subscribe('reports-limit', {limit: 5});
    },
    data: {
      title:'Dashboard',
      reports: function(){
        console.log(Reports.find({}));
        return Reports.find({});
      }
    },
    action: function () {
      if (this.ready()) {
        this.render();
      }
    }
  }); //end this.route
});//end Router map

服务器/publisher.js

Meteor.publish('reports-limit', function(option){
  var limit = options.limit;
  return Meteor.reports.find({}, {sort: {date: -1}, limit: limit});
});

客户端/模板/dashboard.html

<Template name="dashboard">
  <div class="content">
    {{title}}
    <ul>
      {{#each reports}}
        <li>{{_id}}</li>
      {{/each}}
    </ul>
  </div>
</Template>

我没有在 id 中得到任何显示,并且 console.log() 给了我

L…n.Cursor {collection: LocalCollection, sorter: null, _selectorId: undefined, matcher: M…o.Matcher, skip: undefined…} 

结果

这是我安装的包

$ meteor list
accounts-facebook                     1.0.4  Login service for Facebook accounts
bootstrap                             1.0.1  Front-end framework from Twitter
iron:router                           1.0.9  Routing specifically designed for Meteor
meteor-platform                       1.2.2  Include a standard set of Meteor packages in your app
monbro:mongodb-mapreduce-aggregation  1.0.1  Expose mongodb aggregation framework (mapReduce, aggregate and distinct), to SERVER si...
service-configuration                 1.0.4  Manage the configuration for third-party services

我不确定我错过了什么以及我做错了什么。

【问题讨论】:

  • waitOn,不是onWait 并且需要返回订阅:github.com/iron-meteor/iron-router/blob/devel/…
  • 其实你的很多代码都是错的。我强烈建议阅读 Iron Router 指南 - github.com/iron-meteor/iron-router/blob/devel/Guide.md 并继续阅读 Discover Meteor Book - discovermeteor.com
  • @fuzzybabybunny 谢谢,更新了我的代码,我看起来还没有解决我的问题。它仍然给我同样的结果
  • 就像我说的,您的代码中有大量错误。我建议慢慢阅读一些教程以真正了解发生了什么。
  • 您还在 Meteor 光标上使用 console.log,我认为您期望的是一组文档。在这种情况下应该是console.log(Reports.find({}).fetch());

标签: node.js mongodb meteor publish subscribe


【解决方案1】:

一旦你用一个全局变量命名一个集合,例如Reports = new Meteor.Collection('reports');,您应该使用该全局来访问服务器端和客户端的集合。

所以您出版物中的return Meteor.reports.find(); 是错误的。它应该是:

return Reports.find();

查询的其余部分看起来没问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-04
    • 2014-06-16
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 2018-12-26
    相关资源
    最近更新 更多