Question

In my meteor app, I'm trying to register a user on the server, do some work and add some additional properties to the user object passed into onCreateUser, and then go to a 'welcome' page specially adapted to the new user. Using Meteor 7.0.1 and Iron-router (latest)

My client code looks something like this :

// var userId = Meteor.registerUser(registrationOptions);
// Router.go('/welcome/'+userId);

Server code like this

// Meteor.onCreateUser(options, user){
//   if (options.profile) user.profile = options.profile;
    // a 3rd party method outside of Meteor
//   callToOutsideMethod(Meteor.bindEnvironment(function(e, res){
//      user.results = res;
//      // do other stuff
//      user.foo = blah;
//      user.bar = baz;
//      return user;
//     }, function(err){ console.log(err); });

When I return the user object, I'm expect meteor to create the record, then log the new user in...

But what happens is that the screen becomes all distorted for a second, and then we jump to the new url '/welcome/the_new_user_id'...

But it throws an error on the script where I'm trying to access anything aside from user_id. Looking on the Mongo cli, none of the new properties have been added, even 'username' and 'login-tokens' etc do not appear on the new user. And the newly created user has ONLY an _id and nothing else... very weird.

Any ideas on what could be causing this weird behaviour ?

Était-ce utile?

La solution

In your code, you're not actually returning anything inside onCreateUser. The inner function returns some value which then simply stands there.


Solution?

If the inner function (callToOutsideMethod) is synchronous, which it doesn't seem to be, since it uses callbacks – but if it is, your can simply put a return statement in front of it:

return callToOutsideMethod(...

If the inner function is asynchronous, you need to backtrack a bit and, if possible, get the return value of that function before actually creating the user. If that is not possible, see below.


EDIT: Other hooks

Remember that many more hooks than onCreateUser exist, both as part of the API and internally. Don't be afraid to look through the source files and use them to create your own solution; the inner workings of Meteor are relatively simple:

In the case of the onCreateUser hook, it is called inside the function Accounts.insertUserDoc, which is called by the different login services[1]. In your case, you need to be able to continue with the process inside insertUserDoc after your asynchronous stuff is done.

The only way I see how, with the current API, is by redefining the insertUserDoc function (may seem hazardous but similar stuff's been done before) – something along the lines of:

// Save the built-in function first
Accounts.insertUserDocFinal = Accounts.insertUserDoc;

Accounts.insertUserDoc = function (options, user) {
  callToOutsideMethod(some_arguments, function (data) {
    // Manipulate options and user objects?

    Accounts.insertUserDocFinal(options, user);
  });
};


[1] with the options and user objects that is then passed on to onCreateUser (except they don't yet have the properties _id and createdAt).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top