Cannot read property 'session' of undefined

Use this board to ask questions or have discussions with other Profound.js users.
gtyagi
New User
Posts: 2
Joined: Tue Jan 15, 2019 7:08 am
First Name: gajebder
Last Name: tyagi
Company Name: programmers.io
Phone: 9785374546
Address 1: lalkothi
Address 2: jaipur
City: jaipur
State / Province: Rhode Island
Zip / Postal Code: 302015
Country: India
Contact:

Cannot read property 'session' of undefined

Post by gtyagi »

I get the following error "Cannot read property 'session' of undefined"
when i do a - display.lstsfl.replaceRecords(results);
I fetch the data from an api using node-fetch promises and provide the response of the api into replaceRecords.
This error is catched in the catch bloack.

fetch(url, options)
.then(res => res.json())
.then(json => {
console.log(`this is the data object: ${JSON.stringify(json)}`);
if (json.success === false) display.lstsfl.replaceRecords({});
else replaceRecords(json.response.results); // inside this function iam doind a replaceRecords with the correct records
})
.catch(error => {
console.log(' throws error ' + error); // in lof file i get the error : throws error TypeError: Cannot read property 'session' of undefined
});
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: Cannot read property 'session' of undefined

Post by Scott Klement »

This means that there is some code that does something like this:

Code: Select all

   if (myVariable.session == "something") {
   }
And "myVariable" happens to be undefined, possibly because it never was set, or possibly because a bad parameter value was passed for it, etc. But, whatever the case myVariable is undefined, and so you get the message that you can't read the property named 'session' in a variable that is undefined.

(It doesn't need to be an if statement, and the variable doesn't need to be called "myVariable". It could be anything that refers to a subfield named "session")

You'll need to troubleshoot the code and determine what is causing the problem.

If the problem appears to be inside Profound Logic's code, then you will need to send a bug report to support@profoundlogic.com, and explain to them how to reproduce the problem. (They won't be able to do much to help unless they can reproduce it.)
User avatar
Alex
Profound Logic Staff Member
Posts: 233
Joined: Fri Jan 04, 2008 12:10 pm
First Name: Alex
Last Name: Roytman
Company Name: Profound Logic Software
Contact:

Re: Cannot read property 'session' of undefined

Post by Alex »

I see you’re calling Profound.js API like replaceRecords(). Most pjs API are meant to be top-down and should not (and sometime cannot) be called from callback functions or from promises directly, especially for screen-related API. We use a technology called Fibers for this, and what it does is eliminate callback hell and prevent things from running out of sequence.

That's essentially what the problem is with your code. You’re using a promise with .then() and arrow functions ( => ), and mixing it together with the pjs display API.

For something like fetch, I recommend using our pjs.sendRequest() instead. It does the same thing. See: https://docs.profoundlogic.com/x/awI1Ag

If you truly want to use fetch, then you have to wrap() the function to be used top-down with the pjs display API. See these resources for more details:
* https://docs.profoundlogic.com/x/VAL5AQ
* https://docs.profoundlogic.com/x/2YTrAQ
gtyagi
New User
Posts: 2
Joined: Tue Jan 15, 2019 7:08 am
First Name: gajebder
Last Name: tyagi
Company Name: programmers.io
Phone: 9785374546
Address 1: lalkothi
Address 2: jaipur
City: jaipur
State / Province: Rhode Island
Zip / Postal Code: 302015
Country: India
Contact:

Re: Cannot read property 'session' of undefined

Post by gtyagi »

Thanks to both of you.

I applied both of your suggestion and the code worked.
I was missing an error handling case after fetch operation. So got that error with success not defined.
Also i have wrapped my fetch operation in fibre.wrap and calling screen to display after that.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest