Insert initial records afterRegister sets ownerId to null

Hi,
I’m using UserEventHandler afterRegister to insert initial records of the newly registered user. However, when I check the records, the ownerId column is set to null. Shouldn’t this be populated with the objectId of the newly registered user?
Here’s my code.

@Override
public void afterRegister( RunnerContext context, HashMap userProperties, ExecutionResult<HashMap> result ) throws Exception
{
 // add your code here
 Category bMeal = new Category();
 bMeal.setId(UUID.randomUUID().toString());
 bMeal.setName("Before Meal");
 bMeal.setLow(70);
 bMeal.setHigh(100);
 Category aMeal = new Category();
 aMeal.setId(UUID.randomUUID().toString());
 aMeal.setName("After Meal");
 aMeal.setLow(80);
 aMeal.setHigh(140);
 Backendless.Persistence.of(Category.class).save(bMeal);
 Backendless.Persistence.of(Category.class).save(aMeal);
}

And also, it seems that logging in with a social account doesn’t trigger this event.

What would be the best event to handle creation of initial records upon successful registration?

Thanks,
Allen

Hello, Allen!

This behaviour is expected. The “Category” objects are created by not-authorized user, so its ownerId is null.
You can add “login” call before saving these objects. It would init user session and objects would have ownerId.
hope it helps,
Alex

Hi,

I’ve thought about doing a login call but it seems that the password provided in the result object is hashed. Will it work with:

Backendless.UserService.login(email, password, this, true);

Regards,
Allen

Hello,
No, it wouldn’t.

You can set ownerId for target objects manually:

....
String usersObjectId = (String)result.getResult().get("objectId");
aMeal.setOwnerId( usersObjectId );
bMeal.setOwnerId( usersObjectId );
...

It should help.

Hi,

I’ve also tried that but it didn’t work. I am able to fetch the objectId with the correct values however it still comes up null afterwards.

I think it might be related to this.

Regards,
Allen

Hello,

No, it doesn’t relate. Note that ownerId can be set on client, but objectId can not.
Make sure that you call aMeal.setOwnerId, but not aMeal.setObjectId.

Oh I see. Ok I will try this first and get back to you.

Hi,

Updated my code as shown below.

public class GenericUserEventHandler extends com.backendless.servercode.extension.UserExtender {

    @Override
    public void afterRegister(RunnerContext context, HashMap userProperties, ExecutionResult<HashMap> result) throws Exception {
        // add your code here
        String usersObjectId = (String)result.getResult().get("objectId");
        initCategory(usersObjectId);
    }

    @Override
    public void afterSocialRegister(RunnerContext context, Map<String, String> userProperties, SocialType socialType, ExecutionResult<HashMap> result) throws Exception {
        // add your code here
        String usersObjectId = (String)result.getResult().get("objectId");
        initCategory(usersObjectId);
    }

    private void initCategory(String ownerId) {
        Category bMeal = new Category();
        bMeal.setId(UUID.randomUUID().toString());
        bMeal.setOwnerId(ownerId);
        bMeal.setName("Before Meal");
        bMeal.setLow(70);
        bMeal.setHigh(100);

        Category aMeal = new Category();
        aMeal.setId(UUID.randomUUID().toString());
        aMeal.setName("After Meal");
        aMeal.setLow(80);
        aMeal.setHigh(140);

        Backendless.Persistence.of(Category.class).save(bMeal);
        Backendless.Persistence.of(Category.class).save(aMeal);
    }

}

Unfortunately when I checked the data, ownerId is still null.

http://support.backendless.com/public/attachments/d214d596322460aa34cbdc18e9d79294.png</img>

Regards,
Allen

Provide your applicationId, we shall try to investigate the problem.

Application Id: CB0E2106-A46F-6072-FFE3-A73AD8282500

Thanks,
Allen

Try the following:

  1. In class Bootstrap, method onStart: add line
Backendless.Data.mapTableToClass( "Category", Category.class );
  1. In class Category add default no-argument constructor:
public Category() {}

Then recompile and deploy it again.

It seems to be working now when I added that code in Bootstrap. Although I don’t understand what that code means but thank you very much.

Although it doesn’t seem to be working on the afterSocialRegister. I get the following error in the terminal:

SEVERE: com.backendless.BackendlessUser cannot be cast to java.util.HashMap


java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at com.backendless.coderunner.runtime.task.EventInvocationTask.runImpl(EventInvocationTask.java:116)
        at com.backendless.coderunner.runtime.concurrent.ExtendedRunnable.run(ExtendedRunnable.java:26)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: com.backendless.BackendlessUser cannot be cast to java.util.HashMap
        at com.gluconitor.events.user_service.GenericUserEventHandler.afterSocialRegister(GenericUserEventHandler.java:40)
        ... 7 more

Regards,
Allen

That code adds mapping between class properties and table columns on client. As you may know, client sdk is open-source and you can look through implementation on github.
Speaking about social registration handler - we’ve been informed about this issue today and started investigation.

Thanks