[英]Meteor - Display the Email Address of the Logged In User
I am trying to display the email address of the logged in user with Meteor. 我正在尝试显示使用Meteor登录的用户的电子邮件地址。
I am using the command Meteor.user().emails[0].address -- and this works sometimes only. 我正在使用Meteor.user()。emails [0] .address命令-有时仅能使用。 Other times it is undefined. 其他时候它是不确定的。 This is because sometimes the page renders before the User's collections is available. 这是因为有时页面在用户的收藏集可用之前呈现。
However, I am using React and not blaze. 但是,我正在使用React而不是大火。 Every solution online suggests using Meteor.subscribe() in the onCreated part of the template. 每个在线解决方案都建议在模板的onCreated部分中使用Meteor.subscribe()。 But I cannot figure out React's equivalent and I cannot figure out how to wait for the User collection before rendering. 但是我无法弄清楚React的等效对象,也无法弄清楚如何在渲染之前等待User集合。
Updated to use Meteor.autorun which accepts a callback function that runs whenever Meteor's reactive sources update. 已更新为使用Meteor.autorun,它接受在Meteor的反应性源更新时运行的回调函数。
Meteor.subscribe accepts an onReady
optional callback. Meteor.subscribe接受onReady
可选回调。 I would attach to the componentWillMount
lifecycle event on your React component, setup your meteor subscription, and cause a state change once onReady
has fired. 我会在React组件上附加componentWillMount
生命周期事件,设置流星订阅,并在onReady
触发后导致状态更改。 Here is some rough example code; 这是一些粗略的示例代码;
var Foo = React.createClass({
componentWillMount: function() {
var _this = this;
// Setup meteor subscription
Meteor.autorun(function () {
_this.setState({
user: Meteor.user(),
});
})
},
render: function() {
// Render nothing until we have a user
if (!this.state || !this.state.user) {
return null;
}
// Render the address when we have the user
return (
<div>{this.state.user.emails[0].address}</div>
);
}
});
Relevant docs: http://docs.meteor.com/api/pubsub.html#Meteor-subscribe 相关文档: http : //docs.meteor.com/api/pubsub.html#Meteor-subscribe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.