2015-11-02 116 views
1

下面這是我收集的代碼流星的pub/sub問題

Competitions = new Mongo.Collection("competitions"); 

var CompetitionsSchema = new SimpleSchema({ 
    year: { 
     type: String 
    }, 
    division: { 
     type : String, 
     allowedValues: ['Elite', '1st','2nd','3rd','4th','Intro'] 
    }, 
    teams:{ 
     type : [TeamSchema], 
     allowedValues: (function() { 
     return Teams.find().fetch().map(function (doc) { 
      return doc.name; 
     }); 
     }()) //here we wrap the function as expression and invoke it 
    } 
}); 

在ALLOWEDVALUES功能

Teams.find是空的。

在我訂閱的刊物如下

this.route('competitions', { 
    path: '/admin/competitions', 
    layoutTemplate: 'adminLayout', 
    waitOn: function() { 
     return [ 
      Meteor.subscribe('teams') 
     ]; 
    } 
}); 

這是我發佈功能的路由器

Meteor.publish('teams', function() { 
    return Teams.find({},{sort: { 
    points: -1, 
    netRunRate : -1 
    }}); 
}); 

我必須做一些訂閱還有什麼地方呢?

+0

你的發佈函數是什麼樣的? –

+0

只需更新我的問題。 – mohsinali1317

回答

1

您的問題是在這一段代碼:

allowedValues: (function() { 
    return Teams.find().fetch().map(function (doc) { 
     return doc.name; 
    }); 
    }()) //here we wrap the function as expression and invoke it 

調用此頁面加載時。此時,Teams集合在客戶端仍然是空的。您需要等待數據準備就緒。由於您在鐵路路由器中使用waitOn,因此只需將此代碼移動到onRendered回撥即可。

+0

onRendered在模板文件中?像Template.ABC.Rendered?但我在收藏中定義它。 – mohsinali1317

+0

我修好了很多。 – mohsinali1317