onCreateUser / CreateUser do different stuff based on user signing up

I have something like this:

user = {
  password: passwordVar,
  email: emailVar,
  profile: {
    cityLong: cityLong,
    cityShort: cityShort,
    vendor: true,
    vendorVerified: false
  }
}

Accounts.createUser(user, function (error, result) {
});

So I have vendors signing up, which is vendor: true and then I have normal users which are vendor: false.

When I have vendors signing up I also want to add a document to a collection. I have no idea how to do that. With createUser I cannot get the ID of the user immediately but I want to attach the userID to my document.

With onCreateUser I cannot get the users profile immdeiately it seems. I also cannot get those information with a session or based on the route since both don’t work on the server. How can I solve the issue? I’m going crazy :confounded:

Nothing found in options.profile in onCreateUser ?

On the client, this function logs in as the newly created user on successful completion. On the server, it returns the newly created user id.

On the server you can use

Accounts.onCreateUser(function(options, user) {
  console.log('On create user');
  var userID =user._id;
  //Call to create new document
  
  // We still want the default hook's 'profile' behavior.
  if (options.profile)
    user.profile = options.profile;
  return user;
});

It will run every time you create a new user.

http://docs.meteor.com/#/full/accounts_oncreateuser

It worked properly with options.profile :smile: I tried it before like nxcong and olivers described, which didn’t make it possible to access the profile.

1 Like