如何獲取userId
中Meteor.startup
函數的任何想法?我需要它來運行一個循環,每10秒坪,但我得到的是Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
流星在啓動函數中獲取userId
我的代碼:
Meteor.startup(function() {
console.log(Meteor.userId());
});
如何獲取userId
中Meteor.startup
函數的任何想法?我需要它來運行一個循環,每10秒坪,但我得到的是Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
流星在啓動函數中獲取userId
我的代碼:
Meteor.startup(function() {
console.log(Meteor.userId());
});
你可能不會得到Meteor.userId()
在startup
功能,因爲從訂閱的數據(如誰是登錄)將需要一段時間的到來,屆時將啓動完成。
使用Tracker.autorun()
代替:
Tracker.autorun(function() {
if(Meteor.userId()) {
///
}
});
要小心,這將在whever運行用戶登錄也是如此。爲確保它只運行一次,您可以使用可存儲其運行次數的Session
,並在運行次數超過一次時停止運行。
答案錯誤消息明確提及。順便說一下,在嘗試打印之前,您需要檢查是否登錄。
Meteor.startup(function() {
if (this.userId)
console.log(this.userId);
});
以及我嘗試'this.userId',但作爲一種方法,你會嘗試'Meteor.userId()'的方法。反正,似乎工作如此ty –
劃痕。它不發送任何錯誤,但它不會登錄用戶。 'this.userId'總是'undefined'。 –
在Deps.autorun裏面調用'Meteor.userId()'會導致另一個錯誤:'錯誤:流星代碼必須總是在光纖內運行' –
你是否在服務器上這樣做?除非客戶端向服務器發出請求(例如發佈功能,方法或某種呼叫),否則無法在任何地方獲取用戶的ID。因爲沒有辦法區分哪個用戶在沒有與客戶端進行某種形式的通信的情況下登錄,所以在'startup'和Deps.autorun中不可能只在客戶端運行 – Akshat
是的。我是笨蛋。這個流星的東西讓我都分散了一點點大聲笑。 –