Meteor.user()不会等待使用Iron RouteControl在onBeforeAction中加载
我正在尝试加载登录用户的以下用户食谱(如Twitter的home feed)。 我在用铁路由器控制器 我想在订阅和数据中查询用户的以下用户配方,如:Meteor.user()不会等待使用Iron RouteControl在onBeforeAction中加载,meteor,iron-router,Meteor,Iron Router,我正在尝试加载登录用户的以下用户食谱(如Twitter的home feed)。 我在用铁路由器控制器 我想在订阅和数据中查询用户的以下用户配方,如: Recipes.find({ author: { $in: Meteor.user().followings } }, this.findOptions); 但是'Meteor.user()'是未定义的,即使我使用'obBeforeAction'。 所以部分Meteor.subscribe(“following recipes”,
Recipes.find({
author: {
$in: Meteor.user().followings
}
}, this.findOptions);
但是'Meteor.user()'是未定义的,即使我使用'obBeforeAction'。
所以部分Meteor.subscribe(“following recipes”,Meteor.user()。following,@findOptions())
和Recipes.find author:$in:Meteor.user()。以下…
未定义
我是新来的流星,我需要帮助,请帮助我,谢谢
我的全部代码(咖啡脚本):
我不是一个写咖啡脚本的人,但我可以试着解释一下错误
Route dispatch never rendered. Did you forget to call this.next()
in an onBeforeAction?
通读铁路由器文档的自述
来自文档
onRun和onBeforeAction钩子现在要求您调用this.next(),并且不再使用pause()参数。因此,默认行为发生了逆转。例如,如果您有:
Router.onBeforeAction(function(pause) {
if (! Meteor.userId()) {
this.render('login');
pause();
}
});
您需要将其更新为
Router.onBeforeAction(function() {
if (! Meteor.userId()) {
this.render('login');
} else {
this.next();
}
});
这意味着,无论您在何处使用了onBeforeAction函数,如果您想继续处理流,比如在检查用户是否登录后要运行hook(action,onAfterAction),则需要编写this.next()
以继续处理流
Ex代码
Router.onBeforeAction(function() {
if (! Meteor.user()) {
this.render('login');
} else {
this.next();
}
});
在上面的代码中,如果用户是loggedin,则流将继续并执行下一个函数,如action、data、onAfetrAction,否则它将呈现登录模板是因为客户端还不知道用户集合吗? 在waitOn函数中订阅客户端 比如:
this.route('myaccount',{
waitOn: function() {
Meteor.subscribe('experts-all'); // users
},
onBeforeAction: function () {
if (!Meteor.user()) {
Router.go('signin');
}
else this.next();
},
template: 'myAccount',
});
谢谢你的评论,但是我已经定义了next(),所以这不是问题所在。我还在调查和重写一些。
Router.onBeforeAction(function() {
if (! Meteor.user()) {
this.render('login');
} else {
this.next();
}
});
this.route('myaccount',{
waitOn: function() {
Meteor.subscribe('experts-all'); // users
},
onBeforeAction: function () {
if (!Meteor.user()) {
Router.go('signin');
}
else this.next();
},
template: 'myAccount',
});