Meteor.userId()。用户名是"未定义"当用户登录时

时间:2017-11-02 22:09:50

标签: javascript meteor

所以,我的应用程序中有一个登录用户(我自己)。我知道我已登录,因为我的用户名显示在页面中。但是,在我的Javascript中,我无法将我的用户名分配给变量。

我的main.js文件包含:

var uniqueID = Meteor.userId();
var username = uniqueID.username;
console.log("uniqueID = "+uniqueID);
console.log("username = "+username); 

控制台日志显示:

uniqueID = XGX8Qas2ZLCwNYxTH
username = undefined

我根据有类似问题的帖子在js文件中尝试了以下行:

var username2 = Meteor.users.findOne({ _id: Meteor.userId() }).username

这导致了一个彻头彻尾的错误(Uncaught TypeError:无法读取属性'用户名'未定义)

现在,在浏览器控制台中,如果我输入:

Meteor.users.findOne({ _id : "XGX8Qas2ZLCwNYxTH" })

我得到了预期的回复:{_ id:" XGX8Qas2ZLCwNYxTH",用户名:" welehman"}

所以我完全不知道如何输入用户名。我之前实际上有这个工作,但现在我确实需要它,当然它突然不起作用。

1 个答案:

答案 0 :(得分:0)

这里你想要的是整个用户记录,而不仅仅是id:

var uniqueID = Meteor.user(); // Not Meteor.userId()
var username = uniqueID.username; // This will work then

然后立即将uniqueID变量更改为更合适的名称,因为它现在只保存整个用户ID。

相关问题