Code still runs after redirect?

In the on-page enter block, I first want to see if the user is logged in and, if not, redirect to the authLogin page.

I have simplified the logic to constantly redirecting on page enter -

image

I thought once the execution hits the redirect and leaves the page, the rest of the code would not be executed. But that isn’t happening -

I get a bunch of errors because, without a logged-in user, the APIs to get the data don’t run so the variables aren’t defined.

I’m confused about the execution order and overall what is happening here.

Hi @Tim_Jones

If a Codeless block has a bottom connection it means the next block will be executed after the block, of course, if the is not failed with an exception.

In order to interrupt execution you have a few options/approaches, take a look at some of them:

  1. use a condition block with ELSE

  2. use the Return block and go out of the current blocks group

Regards,
Vlad

Thanks for the clarification, @vladimir-upirov.

I’m still having issues with errors that don’t make sense to me. I have everything disabled On Before Page Enter -

On Page Enter now looks like this based on your feedback -

image

The redirect works, and the print statement isn’t running. However, I am still getting error related to the page that the redirect was on. I am not clear why those are being referenced since the browser isn’t on that page any longer -

Thanks,
Tim

do these errors come from the new page logic?

No. They are from the old page. That is why I am confused.

Any ideas? I can’t release this functionality until I sort out why this is happening.

Tim

It looks like there is a code execution order error. In testing, I set On Before Page Enter to redirect. This should be the very first thing to execute, right -

image

In one of the simple models (from the Marketplace), the class list logic prints out a message -

When the page loads, the redirect happens, but the class list logic also fires (along with other code). None of the page variables are set up, because we’re supposed to be leaving the page, so everything errors -

This is similar to the issue I had in another request -

@Glenn_D @mark-piller - Any ideas? I know that backendless is not entirely sequential, but I’m unsure how to diagnose and resolve this.

the On Before Page Enter event is async and it doesn’t block the render phase, so it means the page is being rendered anyway that’s why you can see that components are going to be rendered even if you interrupted the On Before Page Enter event

In order to prevent rendering any inner component on the page you can use the following approach:

  1. in the On Before Page Enter event in case you want to render the page you set some property to the page data, for instance, ready with the value true
  2. for all top-level components on the page you use On Visibility Logic handler you use (data-binding) the. ready value from the PageData
  3. in the UI Designer you switch off the Display setting for all the top-level components to prevent rending them, but they will be rendered only when the ready value is true in the PageData
  4. pay attention to for components with Display=False it still runs its handlers but doesn’t render (create) children and doesn’t display the component

If On Page Before Load is run asynchronously, it’s not really “before load” is it?

That feels like a lot of work when all I want to do is check if a user is logged in and if not redirect them. Is there a better way to do that?

Tim

I tried what you suggested and have reduced the errors from 50 to 4.

I am now stuck that the errors are coming from a component. The page header is used on every page. On every other page, I don’t have to wrap it in a page ready check for it to work.

Again this feels too complicated to do all these checks when I want to do one simple authorization check and leave the page if the user isn’t authorized. This feels like building around a problem with the execution order.

Tim

Hello

You can also correct the location of the error.
Cannot read properties of undefiend
The error occurs because we are trying to take a property from a non-existent thing.

This can be solved in several ways.

  1. Add check for non-existent property


    Just use this construction

  2. Or use an initializing data
    On Before Page Enter

Regards

Hi @viktor.liablin,

All the properties are initialized On Before Page Enter unless the user is not logged in. In that case, the page properties are not initialized because the browser should leave the page before it matters.

For reasons I have still not received a clear answer to, “before” doesn’t apply here.

I can implement solution 1; it just seems redundant and a workaround to the execution order issue.

Please help me with the specific undefined errors I am getting. In this case, there is an error for FirstName -

image

This is the code block -

That makes sense; the user isn’t logged in, so FirstName is null. Are you suggesting I fix it like this -

Tim

No, that didn’t work, but this did -

@Tim_Jones

That feels like a lot of work when all I want to do is check if a user is logged in and if not redirect them.
It is no true.
You can do something like this:



As for your examples:



It’s more correct to do so:

Regards

Thank you for the clarification on checking the current user.

The example of redirecting does not work. If you scroll up, I tried to do a redirect as the first thing On Before Page Enter and still received errors.

I was told the code is running asynchronously, which I do see in testing, so “before” in backendless UI-Builder isn’t really “before” as far as I can tell.

Tim

@Tim_Jones

Sorry, the example of logic for “On Before Page Enter” was not correct. I’ve updated it.

Is the change you made the not?

@Tim_Jones

I added “else”

Hi @Tim_Jones

@vladimir-upirov gave me tips on handling asynchronous loads sometime back… I thought I would summarise the pattern I am using to handle page object definition/references during page loads and data loads to make things a little easier to implement across multiple pages.

Backendless rules:

  1. Any database loads cause components on a page to refresh UI/Logic and data binding
  2. Components that are not displayed don’t fire a UI/Logic or binding refresh
  3. Yep the Page Load events (On Before or On Enter) are not synchronous in operation. Which makes sense if components need to respond to changes. Blocking of events is not possible and is a feature of the UI/Data Binding framework.

The following pattern will help to not get undefined objects during Page Load events as well as redirect someone to another page or show an error if they are not logged in:

  1. Place standard components and reusable components within a parent Workspace block.

  2. At the beginning of the page load hide the Workspace block with Visibility Logic by setting pageLoaded = false. This will hide all page components - refresh of UI/Logic or data binding will not occur. Therefore no undefined object errors will occur during page load.

  3. Validate user, load database, set all objects and values needed.

  4. Show Workspace block (pageLoaded = true) - all refreshes for UI/Logic and data binding will now occur.

  1. Show Spinner during steps 1-4 if it helps to inform users that something is happening during page load.

1 Like