2015-06-01 37 views
0

我有以下途徑:流星 - 鐵路由器:重定向無效參數

Router.route('publication/:_id', { 
    name: 'show_publication', 
    controller: 'PublicationController', 
    action: 'show', 
    where: 'client', 
    waitOn: function() { 

     return [ 
      Meteor.subscribe("publication", new Mongo.ObjectID(this.params._id)), 
      Meteor.subscribe("deal", new Mongo.ObjectID(this.params._id)) 
     ]; 
    } 
}); 

而下面的控制器操作:

// ... 
show: function() { 

    var publication = Publications.findOne({ 
      _id: new Mongo.ObjectID(this.params._id) 
     }); 


    if (publication) { 
     this.render('Publication', { 
      data: publication 
     }); 

    //The hex string is valid but it's not a publication _id 
    } else { 
     Router.go('home'); 
    } 

} 
// ... 

_id參數是一個十六進制字符串,並與我創建ObjectID來檢索出版物。

問題出在參數不是正確的十六進制字符串時。我在控制檯中此錯誤:

Exception in callback of async function: Error: Invalid hexadecimal string for creating an ObjectID 

所以,調用waitOn功能,我想檢查十六進制字符串是有效的,如果不是之前,重定向到主頁。我試圖用onBeforeAction

// ... 
onBeforeAction: function() { 

    try { 
     new Mongo.ObjectID(this.params._id); 
    } catch (e) { 
     Router.go('home'); 
    } 
} 
// ... 

但它沒有奏效。

任何想法?

回答

0
onBeforeAction: function() { 
     try { 
     new Mongo.ObjectID(this.params._id); 
     } catch (e) { 
     Router.go('home'); 
     } 
    } 

,而不是 「onBeforeAction:」 寫在上面的代碼 「數據:」 回調,因爲你waitOn功能等到數據返回

data: function() { 
     try { 
     new Mongo.ObjectID(this.params._id); 
     } catch (e) { 
     Router.go('home'); 
     } 
    }