Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accounts.createUser() is returning undefined on client

Tags:

meteor

I have a SignUp/SignIn Process on my meteor site. I have a form to register new users and a form to login users.

I init users on serverside via

Accounts.createUser({username: "myusername", password: "mypassword"});

and I can login into my dummyusers via my loginform on the client with

Meteor.loginWithPassword(username, password)

I dont know why but for some reason I can´t create new users on clientside. If I call

Accounts.createUser({username: username, password: password});

it returns undefined (safari)

I checked the input strings (username, password) and its not generating the error even pure strings like

Accounts.createUser({username: "test", password: "pass"});

returns undefined on client console.

I published and subscribed my users via:

Server

Meteor.publish('users', function () {
    console.log("Server Log: publishing all users");
    return Meteor.users.find();
});

Client

Meteor.subscribe("users");

I also can count() users on the client console but I cant use Accounts.createUser() so the createUser() function is not even working in my console.

Why I cant use Accounts.createUser() on client/console?

like image 581
Dude Avatar asked Mar 19 '15 19:03

Dude


1 Answers

Accounts.createUser is an asynchronous function on the client because it needs to query the server to know if it actually succeeded (the username or email could already be in use). The return value of an asynchronous function will always be undefined, so the result is expected.

If you start with an empty project and an empty database, you could do:

Accounts.createUser({username: 'dave', password: 'password'});

which will return undefined, however a subsequent call to Meteor.userId() should return a string.

In order to determine if the call succeeded, you will need to supply a callback:

Accounts.createUser({username: 'dave', password: 'password'}, function(err) {
  if (err)
    console.log(err);
  else
    console.log('success!');
});
like image 86
David Weldon Avatar answered Nov 19 '22 08:11

David Weldon