我在 Meteor 中有一个用户个人资料。
我正在使用流路由器。
我想检查用户是否存在于每条路线上。
我努力了
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 的变量填充模板。
我想将 Flow Router 提供的 notFound 配置用于不存在的路由。我想这也可以应用于“不存在的数据”。
因此,如果路由路径是/users/:userId
且具有特定 userId 的用户不存在,则路由器应将该路由解释为无效路径。