I don't still understand how Accounts.createUser/Accouncts.oncreateUser works


#1

I am building my first meteor app, and i am having issues with Accounts.createUser/Accouncts.oncreateUser. This is entirely due to my own lack of knowledge did some google search no satisfying amswer. pls can someone explain to a would be developer in the simplest term possible how to use this Accounts.createUser/Accouncts.oncreateUser (not to bother anyone but examples would be really really really appreciated)


#2

What issues do you encounter? Did you try accounts-ui so you have a working example first?


#3

i already have, accounts-password installed, i am creating my own custom login template so no account-ui, i have written the login line and is working fine, but i don’t seem to be able to wrap my head around this concept Accounts.createUser/Accouncts.oncreateUser, how do they work, i tried writing my codes the didn’t work and cannot find a reason why.

I have this on client/main.js

‘submit #js-loginForm-register’:function(event){

    event.preventDefault();
    var getfirstName = $.trim(event.target.firstName.value),
    getlastName = event.target.lastName.value,
    getUsername = event.target.Username.value,
    getEmail = event.target.Email.value,
    getdateOfBirth = event.target.dateOfBirth.value,
    getpassword = event.target.password.value;
    
    console.log(getfirstName+" "+getlastName+" "+getUsername+" "+getdateOfBirth+" "+getpassword);
    
    Accounts.createUser({
        username: getUsername,
        email: getEmail,
        password: getpassword,
        profile: {
            FirstName: getfirstName,
            LastName: getlastName,
            DateOfBirth: getdateOfBirth
        }
    });
}

and this in server/main.js

Accounts.onCreateUser(function(options, user) {

user.profile.FirstName = options.FirstName;
user.profile.LastName = options.LastName;
user.profile.DateOfBirth = options.DateOfBirth;
return user;
});

And it gives me error on my server pls what am i missing


#4

Please be specific, what error do you receive?

In general: First try running this from the browser console (and disable that server code for testing):

Accounts.createUser({
  email: 'test@example.com',
  password:'testpassword'
}, function(error) {console.log(error);} );

That will show you an error or nothing if the action succeeded. Can you see any result? Does the user get added?


#5

it says undefined in my browsers console. What i mean is Accounts.createUser is used to create user and Accouncts.oncreateUser is used to add extra fields to the user on the db, Accounts.createUser I this from what you just gave me the Accounts.createUser seem fine but the Accouncts.oncreateUser is not.


#6

Ok if createUser is working well then the next step is that onCreateUser: https://docs.meteor.com/api/accounts-multi.html#AccountsServer-onCreateUser

You talked about an error. What error do you receive? I assume, based on your code your issue is here, but I need the error to help you well:

Accounts.onCreateUser(function(options, user) {
  user.profile = {
    FirstName: options.profile.FirstName,
    LastName: options.profile.LastName,
    DateOfBirth: options.profile.DateOfBirth,
  };
  return user;
});

I guess that works. Your error should be visible in your server console and I am not sure but I assume that user.profile does not exists so you cannot set it’s properties.

That being said this code is not very useful because by default Meteor does add this profile data anyway. Do you intend to add other fields as well? Is that the reason you added the onCreateUser?


#7

the error on the server was, “Exception while invoking method ‘createUser’ TypeError: cannot set property ‘FirstName’ of undefined at AccountsServer._onCreateUserHook…”


#8

Great than that’s the issue I expected:

User was defined
User.profile was not.

So if you try to set: user.profile.firstname it will give that error because user.profile was undefined.

Did the example code I sent work?


#9

Thanks I think it worked, server no longer giving error, how do i print all user to browser console


#10

client/main.js

Accounts.createUser({
username: getUsername,
email: getEmail,
password: getpassword,
profiles: {
FirstName: getfirstName,
LastName: getlastName,
DateOfBirth: getdateOfBirth
}
});

server/main.js

Accounts.onCreateUser(function(options, user) {
user.profiles = {
FirstName: options.profiles.FirstName,
LastName: options.profiles.LastName,
DateOfBirth: options.profiles.DateOfBirth
};
return user;
});

this is what the final code look like, am i good.


#11

it worked, thanks alot

i got used console.log(Meteor.user()); and it return my form inputs, really appreciate your time.


#12

Great!

Please read this: https://guide.meteor.com/accounts.html#dont-use-profile

And try to edit your posts here and use the code tags, that makes your posts much more readable. You can do that with:

```javascript
alert('hello world');
```

Which will give:

Alert('hello world);