Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a default user on meteor?

Tags:

node.js

meteor

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: '[email protected]'
    };
    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

like image 758
Camilo Avatar asked Apr 10 '13 21:04

Camilo


People also ask

What is the meteor user () function for?

userId() import { Meteor } from 'meteor/meteor' (accounts-base/accounts_common.js, line 410) Get the current user id, or null if no user is logged in.

What is the name of the package that provides basic user accounts functionality?

`accounts-base` This package is the core of Meteor's developer-facing user accounts functionality.

What is Meteor API?

Meteor is a full-stack JavaScript platform for developing modern web and mobile applications. Meteor includes a key set of technologies for building connected-client reactive applications, a build tool, and a curated set of packages from the Node. js and general JavaScript community.


2 Answers

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',
        }
    });
}
like image 61
SpacePope Avatar answered Oct 04 '22 08:10

SpacePope


this way works for me:

var users=[
   {email: "[email protected]", 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
    });
});
like image 33
lfergon Avatar answered Oct 04 '22 07:10

lfergon