我有一个流星应用程序,我在铁路由器配置中立即调用所有发布功能,如下所示。我只想在用户登录后返回订阅,所以我查看Meteor.userId():
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading',
notFoundTemplate: '404',
waitOn: function () {
if (Meteor.userId()) {
return [Meteor.subscribe('users'),
Meteor.subscribe('company'),
Meteor.subscribe('projects'),
Meteor.subscribe('columns'),
Meteor.subscribe('cards'),
Meteor.subscribe('contents'),
Meteor.subscribe('labels'),
Meteor.subscribe('notifications')];
}
}
});
发布函数具有完全相同的结构,取决于user.companyId,如下所示:
Meteor.publish('cards', function () {
if (this.userId) {
const user = Meteor.users.findOne({ _id: this.userId, companyId: { $exists: true } });
if (user) {
return Cards.find({ companyId: user.companyId });
}
} else {
this.ready();
}
});
我的问题是,当用户注册时,会创建帐户并将companyId保存到用户,但是当他们现在登录时,显示数据的唯一方法是刷新浏览器。我希望它能被反应。
答案 0 :(得分:1)
来自meteor guide:
在客户端上,如果反应函数中的任何内容发生变化,则整体 函数将重新运行,结果非常直观。
然而,在服务器上,反应性仅限于行为 从发布函数返回的游标。你会看到任何 更改匹配其查询的数据,但他们的查询 永远不会改变。
您确实可以按照建议使用reywood:publish-composite,但对于您的简单情况,我认为reactive-publish会更容易启动和运行。
安装软件包,然后将您的出版物包装在this.autorun
:
Meteor.publish('cards', function () {
this.autorun( function() {
if (this.userId) {
const user = Meteor.users.findOne({ _id: this.userId, companyId: { $exists: true } });
if (user) {
return Cards.find({ companyId: user.companyId });
}
} else {
this.ready();
}
});
});