我在流星中有用户配置文件。检查用户是否存在于使用流量路由器的每条路由中
我正在使用流量路由器。
我想检查用户是否存在于每条路线上。
我已经试过
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