Weird bug with Accounts.onCreatedUser


#1

Hey guys,

Here is a weird bug I am having. First here is my code

User = {};

User._defaults = {
  emails : [],
  enabled : true
}

User.defaults = function(){
  var defaults = _.clone(User._defaults);
  return _.extend(defaults,{
    createdAt : new Date()
  });
}

Accounts.onCreateUser(function(options, user){    
  console.log(User.defaults()); 
  // returns {emails: [ { address: 'francis@domain.com', verified: true } ], enabled: true}

  _.defaults(user, User.defaults());

  user.emails.push(user.services.linkedin.emailAddress);

  return user;
});

As you can see, when I call User.defaults() it returns an object with the emails array filled with the email address of the previous new use.

But what is even weirder, is that when I do this :

Accounts.onCreateUser(function(options, user){
  console.log(User._defaults, User.defaults());

  user.enabled = true;
  user.emails = [];
  user.createdAt = new Date();
  // _.defaults(user, User.defaults());

  user.emails.push(user.services.linkedin.emailAddress);

  return user;
})

The logged User.defaults() actually return an object with the emails array empty.

Anyone has an idea what can cause this ??

Thanks !!