Trouble using user service API through hosted script

I suspect I’m doing something fundamental wrong here but cant figure it out. I am trying to use hosted scripts to perform user service operations such as register and login. I am using the hosted script shopping cart example as somewhat of a guide. Here is the code I have:

  1. In index.html the registerUser function:

registerUser = function() {
var resultDiv = $("#resAddItem");
resultDiv.html("");

$.ajax({
    url: window.location.protocol + "//api.backendless.com/B75DC8E3-A4F2-255C-FF29-BE5B2377B400/v1/files/web/scripts/FuTr/scripts/register_test.js",
    type: "POST",
    data: '{"userName":"test@test.com", "password":"abc"}',
    contentType: "application/json",
    success: function (result) {
        resultDiv.html("<strong>Response:</strong><br>"+JSON.stringify(result));
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
  1. In index.html the html source:

<p>1. Register a user.</p>
<button type="button" onclick="registerUser()">Register User</button>

    &lt;div id=&quot;resAddItem&quot;&gt;&lt;/div&gt;
  1. The contents of the register_test.js file:

var Backendless = require( ‘…/libs/backendless.min’ );

exports.run = function( request, response )
{
Backendless.initApp( “B75DC8E3-A4F2-255C-FF29-BE5B2377B400”, “2919B8AD-347C-EDA4-FFAE-42AEDBF85800”, “v1” );

var jsonBody = JSON.parse( request.body );


var userName = jsonBody.userName;
var password = jsonBody.password;

var user = new Backendless.User();
user.email = userName;
user.password = password;
 
try
{
Backendless.UserService.register( user );
}
catch( err )
{
 console.log( "error message - " + err.message );
 console.log( "error code - " + err.statusCode );
}


response.send( user );

}

When I access the index.html file via public URL and click the register user button I get a response that contains the user data that was passed so I know that the username and password data is making it to register_test.js OK. The problem is that when I look at the Users table in the Data tab I never see the new user get created. It seems that somehow the Backendless.UserService.register( user ); call is never being executed. I look in the console and there are no errors, request & response data look as expected. Everything seems to work except for the user actually being created. I have tried a similar test with logging in an existing user and it has failed in the same way, data is successfully passed to the login script file but the login never occurs.

Hi, Greg,
I suspect you’re getting an error in register() call, but you don’t see it as long as you can’t see the console.log() output in hosted scripts - instead of using console.log() I recommend sending the error in response: response.send( err.message ). Moreover, I think the error is that you’re not allowed to use synchronous API in Node.js.
Try the following script for registration:

exports.run = function ( request, response )
{
 var jsonBody = JSON.parse( request.body );
 var userName = jsonBody.userName;
 var password = jsonBody.password;
 var user = new Backendless.User();
 user.email = userName;
 user.password = password;
 var async = new Backendless.Async( 
 function( success )
 {
 response.send( success ); 
 },
 function( failure )
 {
 response.send( failure );
 });
 
 Backendless.UserService.register( user, async );
};

By the way, you don’t need to declare Backendless variable and call initApp() on it - this is already implemented internally for you.

Regards,
Sergey

Thank you Sergey for the very quick response! You were correct about the nature of the error, switching to use the asynch call worked.

Thanks!