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:
- 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);
}
- In index.html the html source:
<p>1. Register a user.</p>
<button type="button" onclick="registerUser()">Register User</button>
<div id="resAddItem"></div>
- 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.