Page 1 of 1

Cannot read property 'session' of undefined

Posted: Tue Apr 21, 2020 7:14 am
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
});

Re: Cannot read property 'session' of undefined

Posted: Tue Apr 21, 2020 10:32 am
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.)

Re: Cannot read property 'session' of undefined

Posted: Tue Apr 21, 2020 10:55 am
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

Re: Cannot read property 'session' of undefined

Posted: Thu Apr 23, 2020 10:46 pm
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.