如何检查Meteor的用户帐号是否为空?

时间:2016-05-14 23:48:15

标签: meteor

我尝试在Meteor应用程序启动时创建默认用户,因此我检查用户集合中是否有任何用户,但它无法正常工作。

我试试这个:

meteor:PRIMARY> db.users.find().count()

这个count()返回0,但在Mongo我有用户:
>> 2
Meteor.users.findOne()

我也试过这个:
preferred_choices 但是返回undefined ......

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您可能会看到在执行代码时服务器未完全加载的竞争条件 - 尝试将代码包装在Meteor.startup中;

if (Meteor.isServer) {
  Meteor.startup(function () {
    if (Meteor.users.find().count() === 0) {
       seedUserId = Accounts.createUser({
          email: 'f@oo.com',
          password: '123456'
       });
    }
  });
}