[solved] Accounts.onCreateUser acceleration : using defer or Collection Hooks

Hi, I use Accounts.onCreateUser to add some values to the user profile

Accounts.onCreateUser((options, user) => {
  const userToCreate = user;
  if (options.profile) userToCreate.profile = options.profile;
  // other things to do HERE
  return userToCreate;

But I would like also to have other things to do, such as inserting/updating some documents in various collections.
The problem is that this takes a few seconds (8/9 seconds) and increases the waiting time for the end user to finish account creation.

Is there a way to run this separately (no dependency with the returned userToCreate) and return the userToCreate faster ?

You can run the processing after returning the value with Meteor.defer()

1 Like

In our app we use the Meteor Collection Hooks package: GitHub - Meteor-Community-Packages/meteor-collection-hooks: Meteor Collection Hooks

So we can do things like this:

Meteor.users.after.insert((_, user) => {
  // ... the code we want to run
1 Like

Thank you @bartoftutormundi nice reminder, I was already using it to add a role :slight_smile:

Meteor.users.after.insert(function (_, user) {
  let role = ['...'];
  Roles.addUsersToRoles(user._id, role);
1 Like

Thank you @rjdavid, indeed it’s good to read more regularly the manual : Core | Meteor API Docs