2016-05-18 38 views
1

我有一個路由器數據函數,它調用Meteor方法將新文檔插入集合。我注意到文檔被插入了兩次,然後我發現每次訪問路由時數據函數本身都會被調用兩次。我無法弄清楚爲什麼會發生這種情況。被調用兩次的流星路由器數據函數

Router.route('/myurl',{ 
    name: 'myurl', 
    path: '/myurl', 
    data: function() { 
     console.log('dupe?'); 
     // the data function is an example where this.params is available 

     // we can access params using this.params 
     // see the below paths that would match this route 
     var params = this.params; 

     // we can access query string params using this.params.query 
     var post = this.params.query; 

     // query params are added to the 'query' object on this.params. 
     // given a browser path of: '/?task_name=abcd1234 
     // this.params.query.task_name => 'abcd1234' 
     if(this.ready()){ 
      Meteor.call('points.add', post, function(error, result){ 
      if(error) 
      { 
       Session.set("postResponse", "failed"); 
      } 
      else 
      { 
       Session.set("postResponse", "success"); 
      } 
      }); 

      return {_message: Session.get("postResponse")}; 

     } 
    } 
}); 

回答

1

我能夠通過將數據下的所有內容移動到Router.onRun鉤子來解決此問題。