Meteor.user() 只返回它的 _id [英] Meteor.user() returns only its _id

查看:51
本文介绍了Meteor.user() 只返回它的 _id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Meteor.loginWithPassword() 登录用户或使用 Accounts.createUser 创建一个新用户(都是客户端)后,我可以在他们的回调中确认Meteor.user() 确实包含所有集合记录的属性.

After logging a user in with Meteor.loginWithPassword() or creating a new one with Accounts.createUser (both client-side), I can confirm in their callbacks that Meteor.user() indeed contains all the set record's properties.

{ _id: "XXX",
  profile: {
     name: "Joe Shmoe",
     thumbnail: "http://www.YYY.com/ZZZ.jpg"
  },
  username: "joeshmoe" }

此外,根据官方文档

默认情况下,当前用户的用户名、电子邮件和个人资料会发布到客户端.

By default, the current user's username, emails and profile are published to the client.

那么,当我尝试访问模板中的这些字段时,谁能告诉我为什么

So, would anyone be able to tell why when I try to access these fields in my Templates thusly

Template.login.user_name = function () {
    return (Meteor.userId() ? Meteor.user().profile.name : '')
};

由于 Meteor.user() 仅返回 {_id: "XXX"} 而没有任何实际属性而失败?IE.用户确实已登录,但用户对象突然丢失/正在隐藏其所有属性.

it fails due to Meteor.user() only returning {_id: "XXX"} with none of its actual properties? I.e. the user is definitely logged in, but the user object suddenly lost/is hiding all of its properties.

有人知道可能是什么问题吗?

Anyone know what the problem might be?

非常感谢.

这发生在 Meteor 0.5.4 上,这是撰写本文时的最新版本.接受的答案确实解决了这个问题;有时 Meteor.userId() 在对象的其余部分从服务器到达之前已经有效.谢谢大家.

this happens with Meteor 0.5.4, the latest version at this time of writing. The accepted answer indeed fixes the issue; sometimes Meteor.userId() is already valid before the rest of the Object has arrived from the server. Thanks everyone.

推荐答案

可能是数据尚未从服务器到达.不是只检查 Meteor.userId,如果检查属性会发生什么?

It's possible that the data has not yet arrived from the server. Instead of just checking for Meteor.userId, what happens if you check for the property?

Template.login.user_name = function() {
  return Meteor.userId() && Meteor.user() && Meteor.user().profile ? Meteor.user().profile.name : "";
}

这篇关于Meteor.user() 只返回它的 _id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆