Problem with onCreateUser


#1

I’m adding (or trying) a new field to user collection when user registers in my app.

I’m doing it like this:

// client side
  register() {
    Accounts.createUser(this.credentials,
      this.$bindToContext((err) => {
        if (err) {
          this.error = err;
        } else {
          localStorage.setItem('foodApp-user', JSON.stringify(Meteor.user()));
          state.go('food');
        }
      })
      );
  }

// server side
Accounts.onCreateUser(function(options, user) {
    	if(!options || !user) {
    		throw new Meteor.Error('Problem to create new user');
    		return;
    	}

    	var user_extend =
    	{
    		publications: 'new value',
    	};
    	_.extend(user,user_extend);

    	return user;
    });

The registration works perfectly without onCreateUser but with this I get this message:

Exception in delivering result of invoking ‘createUser’: TypeError: Cannot read property ‘0’ of undefined
at Scope. (http://localhost:3000/app/app.js?hash=e55b5cb27cbb813dc522046ecd0abbc05ea212c2:3852:43)
at http://localhost:3000/packages/modules.js?hash=0745e066c55ccb0e727b96a31d333d107595c1d7:33499:24
at http://localhost:3000/packages/accounts-base.js?hash=3f6353a90da402397ef0b889e3812a40c0a71704:322:26
at http://localhost:3000/packages/underscore.js?hash=27b3d669b418de8577518760446467e6ff429b1e:794:19
at loggedInAndDataReadyCallback (http://localhost:3000/packages/accounts-base.js?hash=3f6353a90da402397ef0b889e3812a40c0a71704:434:5)
at MethodInvoker._callback (http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:22)
at MethodInvoker._maybeInvokeCallback (http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:3557:12)
at MethodInvoker.receiveResult (http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:3577:10)
at Connection._livedata_result (http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:4742:9)
at onMessage (http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:3385:12)

Couldn’t find anything helpful on internet.

Am I doing something wrong?

Thanks in advance!


#2

Check that return user; isn’t undefined


#3

The return user; is always true, in other case the function will be finished in the first return.
Anyway, I did a console.log in the server and has content.