在用户(Meteor.loginWithPassword()或Accounts.createUser两个客户端)记录用户或创建新用户之后,我可以在他们的回调中确认Meteor.user()确实包含所有设置记录的属性.
{ _id: "XXX",
profile: {
name: "Joe Shmoe",
thumbnail: "http://www.YYY.com/ZZZ.jpg"
},
username: "joeshmoe" }
Run Code Online (Sandbox Code Playgroud)
此外,根据官方文件,
默认情况下,当前用户的用户名,电子邮件和个人资料将发布到客户端.
那么,当我尝试在我的模板中访问这些字段时,是否有人能够说出原因
Template.login.user_name = function () {
return (Meteor.userId() ? Meteor.user().profile.name : '')
};
Run Code Online (Sandbox Code Playgroud)
它失败了,因为Meteor.user()只返回{_id: "XXX"}没有它的实际属性?即用户肯定已登录,但用户对象突然丢失/正在隐藏其所有属性.
谁知道问题可能是什么?
非常感谢.
编辑: 这发生在Meteor 0.5.4,这是撰写本文时的最新版本.接受的答案确实解决了这个问题; 有时Meteor.userId()在Object的其余部分从服务器到达之前已经有效.感谢大家.
Rah*_*hul 11
数据可能尚未从服务器到达.如果您检查属性,会发生什么情况,而不仅仅是检查Meteor.userId?
Template.login.user_name = function() {
return Meteor.userId() && Meteor.user() && Meteor.user().profile ? Meteor.user().profile.name : "";
}
Run Code Online (Sandbox Code Playgroud)