onCreateUser() not working as expected

  Accounts.onCreateUser(function(options, user) {
    user.score = 0;
    user.profile['score'] = 0;
    return user;
  })

This is my code for my onCreateUser. All I want to do is just have a score variable attached to the player. When I try to access it in the client using

Meteor.user().score

or

Meteor.user().profile['score']

both return undefined. If there is a better way to accomplish what I am trying to do? Because right now this is really pissing me off on how it is doing nothing. I am using accounts-password and ian:accounts-ui-bootstrap-3 (alternative to accounts-ui for bootstrap).

Try something like:

Accounts.onCreateUser(function(options, user) {
        user.score = 0;
        if (options.profile)
            user.profile = options.profile;
        return user;
});

Also isnt it Meteor.userId()?

2 Likes

You are probably not publishing that field to the client.

Login to the mongo DB directly and query the DB, I am sure that field is going to be there.

edit:

To publish that detail include this:

Meteor.publish(null, function() {
    if (this.userId) {
         return Meteor.users.findOne(this.userId, {fields: {score: 1}});
    }else{
        this.ready();
    }
})

^^ this will auto publish to logged in users.

(fixed)

1 Like

I checked the mongo db directly and I got this:

{ "_id" : "68Dhv8YkChxzy974f", "createdAt" : ISODate("2015-05-25T03:01:47.353Z"), "services" : { "password" : { "bcrypt" : "$2a$10$ZANyImeyJZGg2rKg1TlvaOWCHHaPbWpi210hCReWWYdrzYb2Om.AK" }, "resume" : { "loginTokens" : [ { "when" : ISODate("2015-05-25T03:01:47.356Z"), "hashedToken" : "ZhD4mIkihOKDBI6eFi8cb56va8aescjekEYh1Lq4tp0=" } ] } }, "username" : "6324rfs", "profile" : {  } }

No sign of any score field. I also added the suggestion from above when creating this account. (Getting tired of usernames lol)

I seriously think that the function is just not being called. Is there a way to log stuff on the server to see if it was run?

Are you calling onCreateUser on the client or server…? Also, check the console.

if (Meteor.isClient) {
  Accounts.ui.config({
    passwordSignupFields: 'USERNAME_ONLY'
});

Meteor.subscribe('userData');

Template.leaderboard.helpers({
  players: function() {
    if (Meteor.user()) {
      console.log(Meteor.user().score);
      return Meteor.users.find({}, {sort: {'score': -1}});
    }
  }
})
}

if (Meteor.isServer) {
  Accounts.onCreateUser(function(options, user) {
    user.score = 0;
    if (options.profile) {
      user.profile = options.profile;
    }
    return user;
  })

  Meteor.publish("userData", function () {
    return Meteor.users.find({}, {sort: {'score': -1}});
  });

  Meteor.startup(function () {
    // code to run on server at startup
  });
}

Meteor.methods({

});

There’s the whole file; it is being called from the server. Just putting the whole thing just in case. (Sorry for poor indents, ctrlc ctrlv messed it up)

Anything being printed out in the console?

The console.log in the leaderboard helper says undefined. And the field is non-existant in MongoDB I think that maybe the account-ui is handling this function for me so it is getting ignored here.

@jrodmanu

This works. If you want to test it, just create a template called hello with a button that has an id of button.

You should check out the msavin:mongol package, its a nice gui to interact with the db.

For me it is saying undefined method ‘scored’ so I think I have problems bigger than just this. Going to start clean and just copy html

Got it all reset and it works perfectly :smiley: Thank you especially for showing me the msavin:mongol; it is very useful.