RESOLVED: Trigger event when new user is created - how to do?


#1

I am using accounts-password and accounts-ui. How can I add an entry to a collection automatically when a new account is created?


#2

This event is called before the new user is created: http://docs.meteor.com/#/full/accounts_oncreateuser

Perhaps that will help.


#3

I want to add a user name to the collection…


#4

Yes, you’ll get the username as part of the user object passed into the function.


#5

so, i could do the following:

Accounts.onCreateUser(function(options, user) {user.profile = options.profile;
// add my code to add record to collection here

  return user;
});

Will this trigger from isClient? Or do I need to put it in a helper?


#6

I have added the following to my Meter.isServer:

  Accounts.onCreateUser(function(options, user) {user.profile = options.profile;
                                         // add my code to add record to collection here
                                         console.log("USER: "+user.username);
                                         console.log("USER: "+user.profile);
                                         console.log("USER: "+user.userId);
                                         return user;
                                        });

Each part comes back as undefined…

I was hoping to get a userId…


#7

Well, for one the ID would be user._id. But if it’s not set at this point you could probably do user._id = Random.id().


#8

How about matb33:collection-hooks? I use the following to add a document to a Targets collection whenever a new account is created.

Meteor.users.after.insert(function () {
    var targetsId = Targets.insert({userId: this._id});
    return {
        _id: targetsId
    };
});

#9

That did it:) Thanks.