2016-05-14 71 views
1

我想在我的Meteor應用程序啓動時創建一個默認用戶,因此我檢查用戶集合中是否有用戶,但它不起作用。如何檢查Meteor的用戶帳戶是否爲空?

我試試這個:

if (Meteor.users.find().count() === 0) { 
    seedUserId = Accounts.createUser({ 
    email: '[email protected]', 
    password: '123456' 
    }); 
} 

這COUNT()返回0,但在蒙戈我有用戶:
meteor:PRIMARY> db.users.find().count()
>> 2

我試試這個太:
Meteor.users.findOne() 但回報undefined ...

我是doi ng錯了嗎?

+0

你是在服務器端還是客戶端執行此操作? – Soren

+0

服務器端@Soren – thur

回答

1

當您的代碼正在執行時,您可能會看到服務器未完全加載的競爭狀況 - 嘗試將代碼包裝在Meteor.startup中,例如;

if (Meteor.isServer) { 
    Meteor.startup(function() { 
    if (Meteor.users.find().count() === 0) { 
     seedUserId = Accounts.createUser({ 
      email: '[email protected]', 
      password: '123456' 
     }); 
    } 
    }); 
} 
相關問題