【问题标题】:Meteor templates loading twice once with collection undefined once with it defined流星模板加载两次,集合未定义一次,定义一次
【发布时间】:2016-05-23 23:00:29
【问题描述】:

所以在寻找堆栈溢出以解决这个问题之后,我遇到了这个https://dweldon.silvrback.com/common-mistakes 和这个https://dweldon.silvrback.com/guards 两者都非常有用。

我的问题是我的一个模板加载了两次,一次是未定义集合,另一次是定义了集合。所以在控制台中我看到了类似的东西:

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

在我试图解决这个问题的过程中,我已经实现了保护,所以我的 Js 文件看起来像这样

TRADEINFO = new Mongo.Collection("trade_info");

Template.E4E_tradeTile.onCreated(function(){
  this.subscribe('users');
  this.subscribe('trade_info');
});

Template.E4E_tradeTile.helpers({
  borrow(){
    console.log(this.tradeID);
    var guard = TRADEINFO.findOne();
    var query = TRADEINFO.find();
    console.log(guard && query);
    return guard && query;
  }
});

然后在我的模板中我读出了这个借用

  <h2 class="no-margins">{{borrow.element}}</h2>
            <small>Borrow</small>

模板似乎总是呈现空白,(没有文本)所以我认为在呈现模板时集合不可用,然后当集合变为可用时它不会更新。

再次感谢您的帮助!

【问题讨论】:

    标签: javascript mongodb meteor meteor-blaze


    【解决方案1】:

    所以事实证明,这两种方法的组合似乎已经解决了这个问题。我将此添加到路线中:

    Router.route('/problem', {
    
      waitOn: function () {
      // return one handle, a function, or an array
      return Meteor.subscribe('trade_info', this.params._id);
      },
    
    action: function () {
      if(Meteor.userId()){
        this.render('problem_page');
      }else {
        this.render('login');
        this.layout('blankLayout');
      }
    }
    

    并使用了警卫!如上所示

    【讨论】:

      猜你喜欢
      • 2011-07-10
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      相关资源
      最近更新 更多