Business Logic Data Event Handler: Backendless.UserService.getCurrentUser() returns null

What s the right way to get the current user in Business Logic Data Event Handlers using JavaScript? This code returns null:


var currentUser = Backendless.UserService.getCurrentUser();

I am performing request on behalf of an Authenticated user. I can see following properties in req.context:

userRoles: [ ‘RestUser’, ‘AuthenticatedUser’ ],
userId: ‘DFAA50BA-5AD5-7C92-FF4B-706AABD21C00’

Hi, Kateryna.

You can get information about current user id from the context of the request.
For example, if you have ‘beforeCreate’ Persistence event handler:

Backendless.ServerCode.Persistence.beforeCreate('*', function(req) {
  const userId = req.context.userId;
  ...
});

Regards, Ilya

But what is the correct way to find user by id? I am trying


Backendless.Data.of(BackendlessUser.class)
	   			.findById(req.context.userId)

and it gives me “BackendlessUser is not defined”
Do I need to use a different syntax? I can’t find a proper way to do it

Please, pay attention to two things.
First, there are no proper classes in JS.
Second, if your write for Node.js you can’t use sync calls.
If we apply this to criteria to your example we’ll get

Backendless.Data.of(Backendless.User).findById(req.context.userId, new Backendless.Async(onSuccess, onFail));

Regards, Ilya

Thank you for your answer. I am using promises:

Backendless.enablePromises();

So the following code works for me


return Backendless.Data.of(Backendless.User).findById(req.context.userId);

Sorry, I just wanted to prevent the possible problem which I saw.
‘enablePromises’ is the better solution.

Regards, Ilya