[英]how to sync pages after logout and login on one of them?
After logout and login I try to click something on another page but see endless spinner due to userId null. How to sync Meteor.userId after changing user?注销并登录后,我尝试单击另一个页面上的某些内容,但由于用户 ID 为 null 而看到无尽的微调器。如何在更改用户后同步 Meteor.userId? ON server side:
在服务器端:
Meteor.publish('myPublish', function () {
if (!this.userId) {
return this.ready();
}
return Collection.find();});
On client side:在客户端:
const handles = [
Meteor.subscribe('settings')
];
const loading = handles.some(handle => !handle.ready());
The Meteor.userId
function is a reactive datasource, which allows you to autorun a Tracker and subscribe, when the userId is there. Meteor.userId
function 是一个反应式数据源,它允许您在 userId 存在时自动运行 Tracker 并订阅。
Classic Meteor code would look like this:经典的 Meteor 代码如下所示:
Tracker.autorun(() => {
if (Meteor.userId() && Meteor.user()) {
Meteor.subscribe('settings') // will only execute, once the user is logged in
}
})
For React you use withTracker
and should include the Meteor.userId
to your bindings:对于 React,您使用
withTracker
并且应该将Meteor.userId
包含到您的绑定中:
export default withTracker(({ id }) => {
const userId = Meteor.userId()
const handles = userId && [
Meteor.subscribe('todos.inList', id),
Meteor.subscribe('otherSub'),
];
const loading = !userId || handles.some(handle => !handle.ready());
return {
loading,
};
})(MyComponent);
The call to Meteor.userId
should activate the internal Tracker computation to re-run a cycle, once it returns a different value (a non-null value, once logged in).对
Meteor.userId
的调用应该激活内部 Tracker 计算以重新运行一个循环,一旦它返回一个不同的值(一个非空值,一旦登录)。
You can also use Meteor.loggingIn
as reactive data source:您还可以使用
Meteor.loggingIn
作为反应数据源:
export default withTracker(({ id }) => {
const loggingIn = Meteor.loggingIn()
const handles = !loggingIn && [
Meteor.subscribe('todos.inList', id),
Meteor.subscribe('otherSub'),
];
const loading = loggingIn || handles.some(handle => !handle.ready());
return {
loading,
};
})(MyComponent);
References:参考:
https://docs.meteor.com/api/tracker.html#Tracker-autorun https://docs.meteor.com/api/tracker.html#Tracker-autorun
https://guide.meteor.com/react.html#using-withTracker https://guide.meteor.com/react.html#using-withTracker
https://docs.meteor.com/api/accounts.html#Meteor-userId https://docs.meteor.com/api/accounts.html#Meteor-userId
https://docs.meteor.com/api/accounts.html#Meteor-loggingIn https://docs.meteor.com/api/accounts.html#Meteor-loggingIn
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.