0

This is branching from my previous question sent earlier Retrieve value from html 'value' attribute. I am now trying to insert a session variable value into field named 'userType' upon user creation. I have kept the insecure package so I can immediately perform Meteor.users.find().count(); in the console. So far, the users are not being created.

Am I inserting the session variable value the correct way, should this session value be inserted server side with Accounts.onCreateUser?

The client js

Template.joinForm.events({
'submit .form-join': function(e, t) {
    e.preventDefault();
    var firstName = t.find('#firstName').value,
    lastName = t.find('#email').value,
    email = t.find('#email').value,
    password = t.find('#password').value,
    username = firstName + '.' + lastName,
    profile = {
            name: firstName + ' ' + lastName,
            userType: selectedUserType
};

    Accounts.createUser({
        email: email,
        username: username,
        password: password,
        profile: profile
    }, function(error) {
        if (error) {
            alert(error);
        } else {
            Router.go('/');
        }
    });
}
});

I have made the 'userType' session variable global, please see as follows...

Template.authJoinType.events({
'click div.join-type-inner': function(e, tmpl) {
    userType = $(e.target).attr("value");
    Session.set('userType', userType);
    selectedUserType = Session.get('userType');
    console.log(selectedUserType);
}
});
Community
  • 1
  • 1
meteorBuzz
  • 3,110
  • 5
  • 33
  • 60
  • Are you storing the value in the Session because it is on a different route? Ultimitlily I am wondering why you don't just grab the value on new user creation. i.e userType = $(".field").val(); – Nate Dec 03 '14 at 14:41
  • Yes it on a different route. The user decides what userType they are by clicking on a particular box. I will bring the user creation to the same route but this value is unknown until the user clicks anyway. So it must be stored either way. – meteorBuzz Dec 03 '14 at 15:04

1 Answers1

1

createUser takes an options object with at most four fields: username, email, password, and profile. You are passing a fifth field which is being ignored. In order to transmit the userType data to the server you will need to add it to the profile object in the call to createUser.


If it's important that userType exist on the root of the user document (instead of in the profile), you can modify it in the onCreateUser callback like this:

client

profile = {
  name: firstName + ' ' + lastName,
  userType: userType
};

server

Accounts.onCreateUser(function(options, user) {
  if (options.profile) {
    if (options.profile.userType) {
      user.userType = options.profile.userType;
      delete options.profile.userType;
    }
    user.profile = options.profile;
  }

  return user;
});

Meteor.publish(null, function() {
  // automatically publish the userType for the connected user
  // no subscription is necessary
  return Meteor.users.find(this.userId, {fields: {userType: 1}});
});
David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • This was vital to fixing my code (createUser explanation). The userType field now gets created in the profile. Now that I wish to move the field, the server code needs a little fix please. There is error thrown. – meteorBuzz Dec 03 '14 at 16:34
  • this deletes the userType located within profile, so thats great so far. However, it is not created in the root user document. – meteorBuzz Dec 03 '14 at 18:52
  • 1
    The code is correct. If you run `meteor mongo` you can see it in the shell. You will not see the extra fields on the client without [publishing them](http://stackoverflow.com/questions/13151879/publish-certain-information-for-meteor-users-and-more-information-for-meteor-use). Again this is a good case for just putting the data in your profile so you don't have to do that step. – David Weldon Dec 03 '14 at 19:07
  • Hi David, are you familiar with iron_router? My publication is correct but the user is not being subscribed to "userData"> Please check my code above that I have added. According to the docs, I have created a global hook for each path so each path should be subscribed to "userData" – meteorBuzz Dec 04 '14 at 12:03
  • Btw, the code has successfully worked I check the mongo shell. Thanks for asserting the valid code. – meteorBuzz Dec 04 '14 at 12:10
  • 1
    I updated the answer with a publish that doesn't need a subscription. Give that a try - it may be easier than modifying your router. – David Weldon Dec 04 '14 at 14:37