【问题标题】:Meteor publish and subscribe with routesMeteor 使用路由发布和订阅
【发布时间】:2016-01-07 21:16:42
【问题描述】:

嘿,我正在尝试在流星中设置链接显示页面,但数据永远不会进入视图。这很正常,但在我删除自动发布后我似乎无法得到它,有什么想法吗?

谢谢

铁路由文件

Router.route('/show/:_id',{
    template: "show",
    name: "show",
    data: function(){
      return Links.findOne({_id: this.params._id});
    },    
    subscriptions: function(){
      return Meteor.subscribe('links', this.params._id);
    }
});

服务器端

Meteor.startup(function() {

    Meteor.publish('links', function(currentLink){
      return Links.find({ _id: currentLink })
    });

});

模板

<template name="show">
  {{info}}
</template>

收藏

Links = new Mongo.Collection("links");

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    在您的路由器中,尝试使用waitOn 参数。 http://iron-meteor.github.io/iron-router/#the-waiton-option

    Router.route('/show/:_id',{
        template: "show",
        name: "show",
        data: function(){
          return Links.findOne({_id: this.params._id});
        },    
        waitOn: function(){
          return Meteor.subscribe('links', this.params._id);
        }
    });
    

    【讨论】:

      【解决方案2】:

      我更喜欢使用助手而不是数据函数。尝试使用帮助器将您的集合连接到您的模板。 {{info}} 将调用下面这段代码中的 info 助手,它返回 Links 集合中的所有对象:

      Template.show.helpers({
          info: function() {
              return Links.find().fetch();
          }
      });
      

      您的 Link 对象是什么样的?它有“信息”字段吗?

      【讨论】:

      • 我以为铁路由器的数据功能会找到链接。 @brettmclain 和链接确实有一个信息字段
      • 链接确实有一个信息字段
      • 我刚刚编辑了我的答案;你是对的。你的 Link 对象是什么样子的,它有一个“信息”字段吗?页面加载后,打开开发者控制台(chrome 中的 F12)并在控制台中输入:console.log(Links.findOne({_id: Router.current().params._id}));
      • 如果什么也没返回,那么有其他事情发生了(this.params._id 不是您期望的那样,或者数据库中不存在该数据,或者您的订阅是失败或尚未准备好)。
      • 不,我已经删除了自动发布,所以我不能这样做@BrettMclain
      猜你喜欢
      • 2017-11-30
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 2016-03-01
      • 2016-11-29
      相关资源
      最近更新 更多