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');
}
}
// ...
但它沒有奏效。
任何想法?