Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Account.createUser call back is not working in meteorjs

I am developing an app with meteor js. I have created one meteor method for creating user. It's showing me following error:-

Accounts.createUser with callback not supported on the server yet.

here is my meteor method

how can i add callback in account.createUser?

Meteor.startup(function () {
    Meteor.methods({
        postForm:function(doc){
            var result = Accounts.createUser({
                username: doc.username,
                password: doc.password,
                email: doc.emails,
                profile: {
                    lastname: doc.lastname,
                    contact:doc.phoneNumber,
                    bdat:doc.bod,
                    address:doc.address
                }
            },function(){
                console.log('hello');
            });
        }
    });
});
like image 349
kishan Avatar asked Jun 18 '15 11:06

kishan


2 Answers

The "yet" in that error message is likely a mistake on the author's part. According to the documentation:

On the server, [Accounts.createUser] returns the newly created user id.

This means that on the server-side, Accounts.createUser is essentially blocking: it waits for the user to be created, and then returns its newly generated id. So "the callback", in that case, is basically anything that follows your createUser statement. You get one value, the user's _id, which you can use to retrieve the inserted user with Meteor.users.find(). And you can catch thrown exceptions if you want to cover errors.

But as David Weldon said, you could basically do that using Accounts.createUser() on the client, which takes a callback. I guess it makes sense if you want to do something server-specific in the "callback" of that creation, but one may also argue that you could do a server method call just for that. (though it would call the server twice in that case, once for the creation, and once for the callback logic)

like image 182
SylvainB Avatar answered Oct 23 '22 06:10

SylvainB


I received exactly the same error message including the word "yet." My complete error message: Exception while invoking method Error: Accounts.createUser with callback not supported on the server yet. Translate that error message as

Hey, Developer, you big dummy, your method call doesn't handle both response and error via callbacks, yet. Please fix your code.

The issue for me was two fold. Like you, I did not adequately account for callback error and response. What that means is, if there is an error somewhere else in the chain of calls, that error WON'T get passed back to you, so you have no idea what's wrong. Fix the call back code first.

Meteor.methods({
    postForm:function(doc){
        try {
          var result = Accounts.createUser({
              username: doc.username,
              password: doc.password,
              email: doc.emails,
              profile: {
                  lastname: doc.lastname,
                  contact:doc.phoneNumber,
                  bdat:doc.bod,
                  address:doc.address
              }
          });
          if(result){
              // are you using roles?
              // Roles.addUsersToRoles(result, doc.roles);  
              return result;
          }
        }
        catch(err){
            return err;
        }
    }
});

Hopefully this will 'fix' the callback not supported error message. And at that time you should be able to see what is really causing your troubles. In my case it was a faulty Accounts.validateNewUser((user) routine that I had copied from a tutorial and forgotten to update to match my data.

Oh, almost forgot... here's sample code to call the method from the client.

Meteor.call('postForm', newUser, function(error, response) {
    if (error) {
        console.log('postForm: Error: ', error);
    }
    if (response) {
        console.log('postForm: Response: ', response);
    }
});

Good luck with this. Info offered here in case anybody gets the "yet" error!

like image 5
zipzit Avatar answered Oct 23 '22 06:10

zipzit