thur
thur

Reputation: 1074

How to check if Meteor's users account is empty?

I'm trying to create a default user when my Meteor application starts, so I check if there is any user in the users collection, but it doesn't work.

I try this:

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

This count() return 0, but in Mongo I have users:
meteor:PRIMARY> db.users.find().count()
>> 2

I try this too:
Meteor.users.findOne() but returns undefined...

What I'm doing wrong??

Upvotes: 1

Views: 176

Answers (1)

Soren
Soren

Reputation: 14718

You may be seeing a race condition where the server is not fully loaded when your code is executing -- try to wrap your code in Meteor.startup like;

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

Upvotes: 1

Related Questions