Trying to get users table in a custom class

Hey,

First post and still very new to Backendless, loving it so far tho!

I am a little but stuck on something where I am not sure whether it’s a bug, or if I am not doing it right. Basically what I want to do is to retrieve the Users table with some relation depth. I got this working, but then I wanted to implement it with a custom class in my code.

So essentially, instead of getting the Users table, I want it to be a CustomUser.

I quickly figured out I could to this by using:
Backendless.Data.MapTableToType( "Users", typeof( CustomUser ) )

But it doesn’t seem to work. I found someone with the same issue a while back, but it doesn’t seem to have an actual solution, but more of a workaround.

So in the end, my code looks as following:

BackendlessUser user = await Backendless.UserService.LoginAsync(username, password);

Backendless.Data.MapTableToType("Users", typeof(CustomUser));

CustomUser loggedInUser = await Backendless.Data.Of<CustomUser >().FindByIdAsync(user.ObjectId, 2);

But I am still getting this exception:

BackendlessException: Table not found by name 'CustomUser '. Make sure the client class referenced in the API call has the same literal name as the table in Backendless console

Any ideas?

Just to clarify what I am doing/want to do:

My users have a relation to a “Company” and a company can have an X amount of active product relations. So I want to get a user with all the relations (2 at the moment, but could be more. We’re still experimenting). From what I understood, it is generally not considered good practice to convert the Users table to something different, and I should use the BackendlessUser class for it, but after logging in, that class does not have any of the relations (will search a bit more on this topic), so instead I get the row of the current user from the Users table, and I want to have that in a custom class that adheres to our standards.

Hi Joey,

Thank you for checking out Backendless.

The Users table is special as it sits in the SYSTEM DATA section in Backendless Console. When you retrieve data from Users using the class-based approach, you have to use the BackendlessUser class. While it may seem restrictive, the use-case you described is possible. There are two ways to make it happen:

  1. Once the user is logged in, you can make an additional API call to retrieve the relations:
    Two Steps Retrieval - Backendless SDK for .NET API Documentation

  2. Alternatively, you can “enrich” the logged-in user object in Cloud Code in the afterLogin event. Here’s an example in Codeless - it enriches the logged in user object with a 1:1 relation from the Address table:

Creating an event handler:

Event handler logic:

Hope this helps.

Mark

Thanks Mark,

I am using your first method (except using the single step retrieval, instead of the 2 step) which gives me exactly what I needed, but I hoped that with that method, I could still use the MapTableToType on the users table to keep things consistent (or easier to use) in our codebase.

Nevertheless, it’s a really minor thing and I will make it work!

Thanks for the response!

Hi Joey,

Glad it worked for you!

Unfortunately, table to class mapping will not work with the Users table. The BackendlessUser class has the GetProperty method that lets you retrieve the relations.

Regards,
Mark