【问题标题】:Unable to receive published Collection from Meteor server side无法从 Meteor 服务器端接收已发布的集合
【发布时间】:2015-04-28 23:11:03
【问题描述】:

这是我的问题

我已经在 MeteorisServer 和 MeteorisClient 包装器之外声明了我的 Collection。

var Items = new Meteor.Collection("items");

然后我用数组 x 和数组 z 插入我的 Items Collection。

Items.insert({
             owner: x,
             post_id: z,
             draft: true
           });

console.log(Items.find().fetch()) 输出到屏幕服务器端时,我在命令行中看到了这一点。

服务器

 { owner:                                                                                                                                                                                                                
   [ 'Sneaker of the Week: \n\nThe women\'s Roshe Flyknit multi-color arrives soon. #roshe http://t.co/1Zfothclmz\n',    

     'Sneaker of the Week:\n\nThe men\'s Roshe Flyknit is now available: http://t.co/frqNCZQbS5 #roshe http://t.co/HOAbnKFUDZ\n',   

       'Modern comfort. The men\'s Roshe Flyknit is now available: http://t.co/zwTWPMpyYK #roshe http://t.co/l7ZLm67sNC\n',     

       'A full-fledged phenomenon, her destiny is to change tennis and the culture, one fallen rival at a time. #techpack http://t.co/TksxyQTUpr\n',                                                                      
    'Her youth conceals an aggressive game of precise strikes and high-tempo performances. #techpack http://t.co/khqgKdZsfv\n',       

      'Always ready, Genie Bouchard is steady crafting her legacy on the tennis court. #techpack http://t.co/U3WTP6Arax\n',                                                                                              
       'Sneaker of the Week:\n\nThe men\'s Dunk CMFT is now available: http://t.co/WFFCBNxICZ http://t.co/OByzzLLdlL\n' ],     


post_id:                                                                                                                                                                                                              
   [ 'http://pbs.twimg.com/media/B-f5AF0IcAAQkZY.jpg',                                                                                                                                                                  
   'http://pbs.twimg.com/media/B-eHJ8tIIAAFUgO.jpg',                                                                                                                                                                  
     'http://pbs.twimg.com/media/B-TZZx7IIAAH9EH.jpg',                                                                                                                                                                  
      'http://pbs.twimg.com/media/B-PBNPkCQAAJq0w.jpg',                                                                                                                                                                  
    'http://pbs.twimg.com/media/B-O_zjaCUAEb6Fj.jpg',                                                                                                                                                                  
    'http://pbs.twimg.com/media/B-O9HhQCAAAIxHM.jpg',                                                                                                                                                                  
    'http://pbs.twimg.com/media/B-ApJnQIgAQisgR.jpg' ],                                                                                                                                                                
   draft: true,                                                                                                                                                                                                          
   _id: '3GW4oqzNHZw9p6yLQ' },              

然后我在服务器端发布这个

// Publish the logged in user's posts
Meteor.publish("posts-recent", function () {
  return Items.find({ owner: x });
});

客户

然后在客户端接收它。

Meteor.subscribe('posts-recent');
var newItems = Items.find();
console.log(newItems);

控制台

不幸的是,这只是输出对文件开头声明的空集合的引用。我的客户端似乎根本无法接收这些数据。此外,当我在控制台检查器中搜索项目时,我被告知它是未定义的。那么有人知道我使用发布和订阅的方式是否有任何问题吗?

【问题讨论】:

    标签: javascript collections meteor insert publish-subscribe


    【解决方案1】:

    Meteor.subscribe 是一个客户端 API,因此它是异步的,因为在您的数据进入浏览器之前,客户端代码无法阻塞主线程。

    尝试使用此解决方法代码引入一个回调,该回调将在订阅被标记为就绪时触发:

    Meteor.subscribe('posts-recent',function(){
      var newItems = Items.find();
      console.log(newItems);
    });
    

    这是 Meteor 初学者经常遇到的常见错误,有许多可用的解决方案可确保您的代码仅在订阅数据可用时运行,最流行的可能是使用 iron:routerwaitOn订阅选项。

    您也可以存储订阅句柄并跟踪何时激活就绪状态:

    var subHandle=Meteor.subscribe("posts-recent");
    Tracker.autorun(function(){
      // this code will run whenever the (reactive) ready method return value changes
      if(subHandle.ready()){
        console.log(Items.find().fetch());
      }
    });
    

    【讨论】:

    • 自动运行功能发生时关联的模板是否会自动刷新?
    • 是的,因为据说这个模板会显示来自 Meteor 集合游标的数据,这是一个反应性数据源,所以它会使用从服务器获取的新数据重新呈现。
    猜你喜欢
    • 2017-08-23
    • 2013-05-21
    • 2015-02-02
    • 1970-01-01
    • 2018-02-03
    • 2014-11-08
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    相关资源
    最近更新 更多