Meteor collections callback not being called

I am trying to asign roles to a user after account creation with no luck

This is my code

  'schoolNew': function(post){
      Accounts.createUser({
       email: post.input_se,
       password: post.input_sp,
       profile: {
          schoolname: post.input_sn,
          schooldescription: post.input_sd,
          schoollocation: post.input_sl,
          schoollogo: post.input_ls,
          schooltelephonenumber: post.input_st
        }
        });
        
        // Accounts.onCreateUser(function (options, user) {
        // var userid = Meteor.users._makeNewID();
        // var userid = user._id;
       // Roles.addUsersToRoles( userid, ['ay','terms','classes','subjects','examinations','cs','cp','re','grading','rc','qrpt','documents','fc','students','teachers','staff','administration','parents','sms','email','mp','as','schools','hc','policy','about','privacy','contact','schooladmin']);
       // console.log(userid);
        // });

      Meteor.users.after.insert(function (userId, doc) {
        var userid = this._id;
        // console.log(userid);
          console.log('after insert user');
       Roles.addUsersToRoles( userid, ['ay','terms','classes','subjects','examinations','cs','cp','re','grading','rc','qrpt','documents','fc','students','teachers','staff','administration','parents','sms','email','mp','as','schools','hc','policy','about','privacy','contact','schooladmin']);
       
        });

    },

Not even console.log('after insert user'); is being called.

A user is added successfully but the after insert hook never fires.

I’m not aware that Meteor.users has something like after.insert.

What about this?

var query = Meteor.users.find({]);
var handle = query.observeChanges({
    added: function(id, user) {
        Roles.addUsersToRoles(id , .......);
    }
});

Straight from the horses mouth https://github.com/matb33/meteor-collection-hooks/issues/135#issuecomment-171357891

This worked

Meteor.users.find().observe({
       added: function(user) {
        var userid = user._id;
        Roles.addUsersToRoles( userid, ['ay','terms','classes','subjects','examinations','cs','cp','re','grading','rc','qrpt','documents','fc','students','teachers','staff','administration','parents','sms','email','mp','as','schools','hc','policy','about','privacy','contact','schooladmin']);
        }
      });

That may be so, but it’s undocumented and undocumented features have the habit of not working in mysterious ways :slight_smile:

1 Like

I’m pretty sure this feature is well documented. http://docs.meteor.com/#/full/observe

Next time, look at which of the two posts I responded to :slight_smile:

1 Like

Have you tried defining the collection hook outside of the schoolNew method?