我在流星中有用戶配置文件。檢查用戶是否存在於使用流量路由器的每條路由中
我正在使用流量路由器。
我想檢查用戶是否存在於每條路線上。
我已經試過
const userRedirect = (context, redirect, stop) => {
let userId = FlowRouter.getParam('userId');
if (Meteor.users.find({ _id: userId }).count() === 0) {
FlowRouter.go('userList');
}
};
const projectRoutes = FlowRouter.group({
name: 'user',
triggersEnter: [ userRedirect ]
});
userRoutes.route('/users/:userId', {
name: 'userDetail',
action: function (params, queryParams) {
BlazeLayout.render('default', { yield: 'userDetail' });
},
});
,但它不工作。
我想這是因爲我沒有訂閱用戶集合。
我該如何在路線中做到這一點?我應該使用
const userRedirect = (context, redirect, stop) => {
let userId = FlowRouter.getParam('userId');
// subscribe to user
Template.instance().subscribe('singleUser', userId);
// check if found
if (Meteor.users.find({ _id: userId }).count() === 0) {
FlowRouter.go('userList');
}
};
編輯
我曾嘗試在模板中檢查與替代
Template.userDetail.onCreated(() => {
var userId = FlowRouter.getParam('userId');
Template.instance().subscribe('singleUser', userId);
});
Template.userDetail.helpers({
user: function() {
var userId = FlowRouter.getParam('userId');
var user = userId ? Meteor.users.findOne(userId) : null;
return user;
},
});
,但它只是填充模板具有可變user
要麼是用戶對象或null 。
我想使用流路由器提供的notFound配置來存在不存在的路由。我想這也可以應用於'不存在的數據'。
因此,如果路由路徑爲/users/:userId
並且具有特定userId的用戶不存在,則路由器應將該路由解釋爲無效路徑。
你要做的模板層上的檢查,所以在主要佈局在這裏做的檢查是好的指南:https://kadira.io/academy/meteor-routing-guide/content/介紹流程路由器 –
我已閱讀指南,但我沒有看到它提及如何在流路由器中使用notFound配置。我希望應用程序在訪問配置文件路由時不存在用戶不存在的模板。 – Jamgreen