15

I want to create an admin user if no users exist. I tried it on a js file inside the server folder

Meteor.startup(function () {
  if(!Meteor.users.find().count()) {
    var options = {
      username: 'admin', 
      password: 'default-password', 
      email: 'admin@example.com'
    };
    Accounts.createUser(options);
  }
});

This is the error that meteor show on the console

Error
    at app/packages/livedata/livedata_common.js:143:26
    at /Users/camilo/Documents/Proyectos/IM/interno/.meteor/local/build/server/server.js:282:7
    at Array.forEach (native)
    at Function._.each._.forEach (/Users/camilo/.meteorite/meteors/meteor/meteor/0ffea1c4c308ed24906984f99b13b8fca5a0956c/dev_bundle/lib/node_modules/underscore/underscore.js:79:11)
    at run (/Users/camilo/Documents/Proyectos/IM/interno/.meteor/local/build/server/server.js:227:7)
=> Exited with code: 1

I'm doing something wrong or this is a meteor bug?

I'm using meteor 0.6.1 and node.js 0.9.9

Camilo
  • 2,844
  • 2
  • 29
  • 44
  • There is a similar question http://stackoverflow.com/questions/11629759/how-can-i-create-users-server-side-in-meteor but doesn't solve this problem. – Camilo Apr 10 '13 at 21:10
  • Do you have the accounts-password and accounts-base packages in? – Tarang Apr 10 '13 at 21:21
  • Yes. I can create users from client, login, logout, etc. – Camilo Apr 10 '13 at 22:01
  • Is there more data on what the error is? the error might be from something else? – Tarang Apr 11 '13 at 05:52
  • It's the only info I got on the console. I assigned the response of Accounts.createUser to a variable and pass it to console.log, but the application crashed before createUser return something. – Camilo Apr 11 '13 at 14:06

4 Answers4

32

I would suggest a /server/fixtures.js file. In this file, you can add your default user creation as such:

if ( Meteor.users.find().count() === 0 ) {
    Accounts.createUser({
        username: 'username',
        email: 'email',
        password: 'asdfasdf',
        profile: {
            first_name: 'fname',
            last_name: 'lname',
            company: 'company',
        }
    });
}
user456584
  • 86,427
  • 15
  • 75
  • 107
SpacePope
  • 1,423
  • 1
  • 15
  • 21
  • 2
    This code would run at least once every time Meteor is started right? Potentially once for each thread? I guess it wants to be wrapped in a Meteor.startup() call as I don't think `fixtures.js` has any special significance as a file name. – chmac Nov 28 '14 at 15:47
  • 2
    Yes, this would run every time at startup. Wrapping in a Meteor.startup() can't hurt, and would provide a little insurance against being accidentally run, I suppose. No, the filename fixtures.js has no significance. I think I probably got the name from the Discover Meteor book? – SpacePope Sep 09 '15 at 19:14
5

this way works for me:

var users=[
   {email: "dgra@gmail.com", username: "gra", name: "gra", roles:['admin']}
];
_.each(users, function(user){
    Accounts.createUser({
        email: user.email,
        password: "admin",
        profile: {username: user.username},
        profile: {name: user.name},
        roles: user.roles
    });
});
lfergon
  • 963
  • 15
  • 27
1

I've never found any problem on creating a user on Meteor. This is my coffeescript code:

Meteor.startup ->
  if Meteor.users.find.count() is 0
    options =
      email: 'email@example.com'
      password: 'pass'
    Accounts.createUser(options)
Nathan Lippi
  • 4,967
  • 5
  • 25
  • 29
handmade
  • 201
  • 3
  • 3
0

After some time, I tested again the above code with meteor 0.6.4 and it worked without problem. Probably was an issue with meteor 0.6.1 or I accidentally solved the problem somewhere in the code.

Camilo
  • 2,844
  • 2
  • 29
  • 44