Object set in On Before Page Enter - But throws error "Cannot read properties of undefined"

In On Before Page Enter I set logid in parcel in page data -

I call the function to set all the objects, then print everything in page data. This should happen before anything else, right?

However, this is the error I am getting. The error happens before the print -

This is where I am calling logid, from the simple modal component -

What am I missing?

Could you add print in the Content Logic to see what’s in Page Data? This should also help in understanding the timing when the Content Logic is executed.

This is what I added -

This is what happened -

Is the simple modal code running before On Before Page Enter?

It is not clear to me which print statement is coming from which part of logic. Adding some custom text to a print statement would be make it easier to understand.

Yes, of course.

Changes -
image

Results -

Hi,
I think I had a similar issue. I found that "on before page enter or “on page enter” had no difference in refreshing the “data/page model”. i.e. I found the data/page model is refreshed “on page enter” or “on before page enter”. It didnt make a difference.

In my case I found that the data model was refreshed twice. The data model is refreshed anytime you set an object to a get data block like “Get Object by Id from table”. If you do this within the “on page enter” or “before page enter” I noticed that the data/page model was refreshed twice - when you set the object to a returned data table and then also at the end of the “on before page enter” or “on page enter” events. Therefore I was having my data bindings or content logic triggered twice when I thought I was setting things up in the “before page load” and therefore not triggering page events.

I may be wrong but my summary is:

  1. No difference in data/page refresh when using “on page enter” or “before page enter”.
  2. You need to be careful in setting the priority order and types of objects that are set/created. As creating some objects will refresh the page/data model and therefore trigger data binding and content logic before you have even established the objects.
  3. To test this put a print statement inside the “Content Logic” section and watch the printed out order of your various print statements. In my case I saw the content logic being refreshed twice since I was setting an object to a “Get data block” within the “on page” or “before page” events - it didnt matter.

Hi @Glenn_D,

How did you solve this problem? If I can’t set objects in On (Before) Page Enter and then use them elsewhere on the page, I don’t know what to do.

Tim

Hi @Tim_Jones

For this particular issue and until it’s fixed (I think it’s a bug) don’t get logid inside content logic. Instead return the value of logid using a data binding value of: parcel.logid

It returns the same thing but using just data binding variables (instead of code in the logic area) it does not throw the error. It only works in this case because you don’t want to execute more complex logic before you return a value.

This is not a fix but a workaround. I think. Hope it works.

Hi @Tim_Jones,

Also put a print before calling PageDataObjects. This will tell you if a data/page refresh is happening inside your function when you are initiating your objects (the object with address, name or the parcel object creation etc).

I don’t know your whole app setup and I hate to say this but another fix could be to move the parcel object creation to the top/first thing in the function.

Also if you want to analyse things further you can put prints before each create object in your function to see if/how/when calling the create objects triggers the page/data refresh and you will nicely trace at what point the content logic code is throwing an error.

However, like I said in the previous post placing parcel.logid in the data binding field should resolve what you want but isn’t the complete fix/answer.

Would you be willing to show me a screenshot of what you’re talking about? I’m not sure how to do what you’re suggesting.

Moving the object creation to the top of the function worked. That was a lot of time wasted.

I am still unclear why it doesn’t complete before other things start running.

1 Like

Hi @Tim_Jones,

Good to hear that the error was solved by moving the creation of the parcel object to the top of the function. I would be interested to see the whole function, so we might be able to see why its refreshing the page before the parcel object is defined. If you want to share?

To answer your other Q and demo what I mean by using “direct” data binding instead of content logic code I believe this would return the logid you are looking for without throwing an error message, irrespective of where the parcel object declaration is running from in the function.

Hope this helps - even if it is sorted this may help with understanding how to use databinding.

Thanks, @Glenn_D. Happy to share the function. All it does is set up page objects. This is a shipping application, so there are some addresses, the parcel, and a shipment. Plus settings, modal visibility, etc. A Settings table stores API keys, etc., so there is one API call to get those.

Thank you for the screenshot and the annotations; that was very helpful. Is there a benefit, other than this specific workaround, to using a direct reference to the pageData object?

Hi @Tim_Jones,

I am pretty confident that the API service is what does the data/page refresh. The logic happens like this (I think):

  1. API Service runs - database is queried and data is retrieved.
  2. The data/page is refreshed. This happens whenever a database query occurs.
  3. As a result. All events on the page are triggered. Including your content logic code. This happens so that all items on the page are updated.
  4. Since your content logic code references the parcel object but the refresh of the page has occurred before the parcel object is defined you get an error.
  5. Note that at the end of the “On Before Page Enter” the data/page is refreshed again (by default). Which is why you get 2 sets of print statements in what Mark asked you to do.

@mark-piller I had similar issue to what @Tim_Jones is having in this thread. Moving the definition of objects before “data retrieval calls” was the work around to avoid “undefined error” messages from “event handlers”.

I had the same question as Tim’s initial question in this post.

  1. Should there be no data/page refresh activity inside or at the completion of the “On Before Page Enter” event. This would enable the developer to setup all object declarations and databinding/logic code without having to worry about order of object definitions. Leave data/page refresh activity to the “On Page Enter” event.

  2. If the first option is not possible then since data/page refreshes happen at the end of the “On Before Page Enter” and also at the end of the “On Page Enter” events by default then maybe stop the fact that data/page refresh happens inside both the “On Before Page Enter” and “On Page Enter” when a database is queried.

Hope the above makes sense.

Regards
Glenn

Good suggestions @Glenn_D!