簡體   English   中英

用戶登錄時,Meteor.userId()。username為“ undefined”

[英]Meteor.userId().username is “undefined” when user is logged in

因此,我的應用程序中有一個登錄用戶(我自己)。 我知道我已經登錄,因為我的用戶名顯示在頁面中。 但是,在我的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

這導致了徹底的錯誤(未捕獲的TypeError:無法讀取未定義的屬性“用戶名”)

現在,在瀏覽器控制台中,如果輸入:

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

我得到了預期的響應:{_id:“ XGX8Qas2ZLCwNYxTH”,用戶名:“ welehman”}

所以我完全不知道如何輸入用戶名。 我實際上曾經有過這項工作,但是現在我確實需要它,當然它突然不起作用了。

您想要的是整個用戶記錄,而不僅僅是ID:

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

然后,立即將uniqueID變量更改為更合適的名稱,因為它可以容納整個用戶,而不僅僅是現在的ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM