Can't update an existing User using the UserService API

I’m testing out this API but I get stuck while trying to update a column in the Users table. What am I doing wrong?


// Application Settings
var APPLICATION_ID = "XXXXXXXX-B378-1BB8-FF42-XXXXXXXXXX",
   SECRET_KEY = "XXXXXXX-45FC-1DF8-FF9D-XXXXXXXXXXXXX",
   VERSION = "v1";   //default application version;
 
Backendless.initApp( APPLICATION_ID, SECRET_KEY, VERSION );


var loggedInUser;


// Utility functions
function userRegistered( user )
{
 console.log( "user has been registered" );
}


function userUpdated( user )
{
  this.loggedInUser = user;
  console.log( user.name + " has been updated" );
}
 
function userLoggedIn( user )
{
  loggedInUser = user; 
  //updateUser();
  console.log( "user has been logged In" );
}


function userLoggedout()
{
 console.log( "user has been logged out" );
}




function gotError( err ) // see more on error handling
{
  console.log( "error message - " + err.message );
  console.log( "error code - " + err.statusCode );
}


// Object declarations
var user = new Backendless.User();
    user.name = "Darel Johnson";
    user.password = "watchingya";
    user.email = "dareljohnson@msn.com";
    user.username = "dejohnny2";


// Functions
function registerUser(param)
{
  user = param || {};
  this.name = user.name.replace(/\s/g, '') || null;
  this.username = user.username.replace(/\s/g, '') || null;
  this.password = user.password.replace(/\s/g, '') || null;
  this.email = user.email.replace(/\s/g, '') || null;
  
  /* Register new user */
  Backendless.UserService.register( user, new Backendless.Async( userRegistered, gotError ) );
}


registerUser(user);


// Login user
var login = "dareljohnson@yahoo.com";
var password = "watchingya";
Backendless.UserService.login(login, password, new Backendless.Async( userLoggedIn, gotError ));




function updateUser(param)
{
  user = param || {};
  this.name = user.name.replace(/\s/g, '') || null;
  this.username = user.username.replace(/\s/g, '') || null;
  this.password = user.password.replace(/\s/g, '') || null;
  this.email = user.email.replace(/\s/g, '') || null;
  
  Backendless.UserService.update( user, new Backendless.Async( userUpdated, gotError ) );
}


user = Backendless.UserService.CurrentUser();


user.name = "dejohnny";
user.email = "dareljohnson@yahoo.com";
user.password = "watchingya";


updateUser(user);


Hi Darel,

Are you sure the login goes through after you register the user?

Take a look, here’s the code to register the user:

var user = new Backendless.User();
user.name = "Darel Johnson";
user.password = "watchingya";
user.email = "dareljohnson@msn.com";
user.username = "dejohnny2";


function registerUser(param)
{
user = param || {};
this.name = user.name.replace(/\s/g, '') || null;
this.username = user.username.replace(/\s/g, '') || null;
this.password = user.password.replace(/\s/g, '') || null;
this.email = user.email.replace(/\s/g, '') || null;


Backendless.UserService.register( user, new Backendless.Async( userRegistered, gotError ) );
}



registerUser(user);

And this is the code to login. Notice the email address below is different than the email address in the code above. The login should fail and as a result, the subsequent update will fail too.

// Login user
var login = "dareljohnson@yahoo.com";
var password = "watchingya";
Backendless.UserService.login(login, password, new Backendless.Async( userLoggedIn, gotError ));

Well yes and no. I made the email addresses the same, but no change in the way the app behaves. Since the user already exist on the server, I get an error message that says, “Unable to register user. User already exists.”

After that, when the Backendless.UserService.login() is fired, I can see that the user is logged In.

However, the code doesn’t let me update the same user that was previuosly registered.

In your code, do you need to update the same user which is currently logged in? Or one user needs to update another user?

I would like to see both cases.

I made the changes to support the first use case. It is working now.

Mark,

What did you change?

Here’s a complete listing of the code minus the initApp part:

var loggedInUser;


// Utility functions
function userRegistered( user )
{
 console.log( "user has been registered" );
}


function userUpdated( user )
{
  this.loggedInUser = user;
  console.log( user.name + " has been updated" );
}
 
function userLoggedIn( user )
{
  loggedInUser = user; 
  //updateUser();
  console.log( "user has been logged In" );
  
  var user1 = new Backendless.User();
  user1.name = "dejohnny";
  user1.email = "dareljohnson@msn.com";
  user1.password = "watchingya";
  Backendless.UserService.update( user, new Backendless.Async( userUpdated, gotError ) );
}


function userLoggedout()
{
 console.log( "user has been logged out" );
}




function gotError( err ) // see more on error handling
{
  console.log( "error message - " + err.message );
  console.log( "error code - " + err.statusCode );
}


// Object declarations
var user = new Backendless.User();
    user.name = "Darel Johnson";
    user.password = "watchingya";
    user.email = "dareljohnson@msn.com";
    user.username = "dejohnny2";




function createUser(param)
{
  user = param || {};
  this.name = user.name.replace(/\s/g, '') || null;
  this.username = user.username.replace(/\s/g, '') || null;
  this.password = user.password.replace(/\s/g, '') || null;
  this.email = user.email.replace(/\s/g, '') || null;
  
  return user;
}


// Functions
function registerUser(param)
{
  user = param || {};
  this.name = user.name.replace(/\s/g, '') || null;
  this.username = user.username.replace(/\s/g, '') || null;
  this.password = user.password.replace(/\s/g, '') || null;
  this.email = user.email.replace(/\s/g, '') || null;
  
  /* Register new user */
  Backendless.UserService.register( user, new Backendless.Async( userRegistered, gotError ) );
}


//registerUser(user);


// Login user
var login = "dareljohnson@msn.com";
var password = "watchingya";
Backendless.UserService.login(login, password, new Backendless.Async( userLoggedIn, gotError ));




function updateUser(param)
{
  user = param || {};
  this.name = user.name.replace(/\s/g, '') || null;
  this.username = user.username.replace(/\s/g, '') || null;
  this.password = user.password.replace(/\s/g, '') || null;
  this.email = user.email.replace(/\s/g, '') || null;
  
  Backendless.UserService.update( user, new Backendless.Async( userUpdated, gotError ) );
}

Thanks, Mark that worked!

But… do you have an code example that shows how to build a simple web page with user registration, update account, and logout?

Yes, of course, and you have it too… ))

Use the new Code Generation feature available directly in the console. Click “Code Generation”, select JavaScript, click all the checkboxes you want to get the code generated for and then click Download project.

alternatively,

check out the user service example included into the SDK for JavaScript

The Code Generation option is the one I would recommend.

Regards,
Mark

Mark,
I didn’t know you could generate skeleton code in Backendless. Nice work!

Thanks again.