JavaScript User Registration Return Objects Undefined

I am sending user registration to a custom Business Logic function. I am using the asynchronous user registration found in the JavaScript docs.

When I call the function I can see that the user is created and the signUpSuccessful() function is called when the user is successfully register. When I try and print the “user” the console shows it is undefined. The odd this is that the signUpError() function is also called showing that there was an error when registering the user. The full CodeRunner Console read out is below.


20:03:59.019 - [DFF8B34A-A1A4-9565-FF19-2D632D9EE200] New task arrived!
user has been registered
undefined
I am in the signUpError
20:03:59.034 - [DFF8B34A-A1A4-9565-FF19-2D632D9EE200] Processing finished
20:03:59.034 - Error: Cannot read property 'message' of undefined

The code for my custom Business Logic function is below.

Backendless.ServerCode.customEvent('signUpUserInCloud', function(req) 
{
  // Defined for for the method used below to sign up the user. 
  function signUpSuccessful( user )
  {
    console.log( "user has been registered" );
    console.log(user);


    var response = { "test" : "This Worked!" };
    return response;
  }


  // Defined for for the method used below to sign up the user. 
  function signUpError( err ) // see more on error handling
  {
    console.log("I am in the signUpError");
    console.log( "error message - " + err.message );


    var response = { "test" : "Error!" };
    return response;
  }


  // Create a new Backendless User and assign the arguments passed from the mobile device.  
  var user = new Backendless.User();
  user.username = req.args.username;
  user.firstName = req.args.firstName;
  user.lastName = req.args.lastName;
  user.password = req.args.password;
  user.email = req.args.email;
  
  // Call the JavaScript function to sign up the user.  The sign up will be successful or create an error.  The
  // functions above are called accordingly.
  Backendless.UserService.register( user, new Backendless.Async( signUpSuccessful(), signUpError() ) );
});

Any help would be appreciated.

Jon,

When you create the async handler, instead of passing references to the signUpSuccessful and signUpError functions, you actually call them. This explains why you see both outputs in console.

Backendless.UserService.register( user, new Backendless.Async( signUpSuccessful(), signUpError() ) 

The proper way to create a callback is:

Backendless.UserService.register( user, new Backendless.Async( signUpSuccessful, signUpError ) 

Mark

Thanks Mark. Sorry for missing something in the docs. I appreciate the help.