Auto increment user ID with before register event handler

Backendless Version (3.x / 5.x, Online / Managed / Pro )

Online

Client SDK (REST / Android / Objective-C / Swift / JS )

JS

Application ID

683AD1B0-7032-ED2E-FFD2-D9823B2A7200

Expected Behavior

Please describe the expected behavior of the issue, starting from the first action.

On registering new user using JS SDK, add new parameter userIdCounter to user object and write this modified object to Users table.

userIdCounter value must be uniq autoincremental number, received from userId atomic counter.

For this I’ve generated ‘before register’ event handler, wich js code is:
/**
* @param {Object} req The request object contains information about the request
* @param {Object} req.context The execution context contains an information about application, current user and event
* @param {Object} req.user
*/
Backendless.ServerCode.User.beforeRegister(function(req) {
//add your code here
return Backendless.Counters.incrementAndGet( ‘userId’ )
.then( function( result ) {
req.user.userIdCounter = result;
console.log('req.user.userIdCounter: ', req.user.userIdCounter);
})
}, true);

But it not works.

Actual Behavior

What I see in debug log:

11:53:56.320 Starting Debug Code Runner...
11:53:56.320 Building Model..
11:53:56.334 ServerCode Model built in 14ms
11:53:56.334 Event handlers (1):
11:53:56.334   user.beforeRegister (async) (handlers/user/beforeRegister.js)
11:53:56.335 Connection to Redis...
11:53:56.959 Registering Code Runner on https://api.backendless.com
11:53:57.844 Runner successfully registered.
11:53:57.845 Registering Model on https://api.backendless.com
11:54:03.230 Model successfully registered
11:54:03.230 Waiting for Server Code tasks..
11:54:41.113 [33d26fde-eba3-4cd5-9a2a-91abdc8e14ed] New task arrived!
11:54:41.123 [33d26fde-eba3-4cd5-9a2a-91abdc8e14ed] [INVOKE HANDLER] user.beforeRegister (async)
11:54:41.804 req.user.userIdCounter:  1017
11:54:41.805 [33d26fde-eba3-4cd5-9a2a-91abdc8e14ed] Processing finished

Then in Users table I see new user, but userIdCounter is empty.
User register API call returns User object, where userIdCounter value is null:
User {___class: “Users”,
lastLogin: null,
userStatus: “EMAIL_CONFIRMATION_PENDING”,
created: 1604998481000,
ownerId: “989A9391-A4AC-438B-901C-358815FF0DDD”, …}
blUserLocale: “en”
created: 1604998481000
email: “testuser@testdomain.tld”
lastLogin: null
name: null
objectId: “989A9391-A4AC-438B-901C-358815FF0DDD”
ownerId: “989A9391-A4AC-438B-901C-358815FF0DDD”
socialAccount: null
updated: null
userIdCounter: null
userStatus: “EMAIL_CONFIRMATION_PENDING”___class: "Users"proto: Object

Reproducible Test Case

Please provide a simple code that could be run in a new clean app and reproduce the issue.
JS code of before register event handler:

/**
* @param {Object} req The request object contains information about the request
* @param {Object} req.context The execution context contains an information about application, current user and event
* @param {Object} req.user 
*/
Backendless.ServerCode.User.beforeRegister(function(req) {
  //add your code here
  return Backendless.Counters.incrementAndGet( 'userId' )
   .then( function( result ) {
    req.user.userIdCounter = result;
     console.log('req.user.userIdCounter: ', req.user.userIdCounter);
   })
}, true);

Hello @Mansur

Hi. We are looking into your problem.
We’ll answer during this day.

Looks like I’ve found the solution :wink:
In my case, Backendless.ServerCode.User.beforeRegister method works only in synchronous mode, i.e. “Non-blocking:” switch must be disabled on event handler creation dialog form.
Correct and working JS in my case is:
/**

  • @param {Object} req The request object contains information about the request
  • @param {Object} req.context The execution context contains an information about application, current user and event
  • @param {Object} req.user
    */
    Backendless.ServerCode.User.beforeRegister(function(req) {
    //add your code here
    return Backendless.Counters.incrementAndGet( “userId” )
    .then( function( result ) {
    req.user.userIdCounter = result
    })
    });