Query Error I can't work out

I have a pricing plan query which I use to determine what options to show in the menu, according to the plan the user has subscribed to.

I had to include first and second plan name as I found previous subscriptions would still impact the results even once the plan was cancelled (any suggestions around this much appreciated).

Recently, I have noticed that on preview and the live site the code is not working, and the error message I get is “uncaught (in promise) TypeError: cannot read property ‘name’ of undefined”.

The confusing part is when I delete the affected line of code, the error still shows.

I have screenshot the console log on the live page, and copy/pasted the code I am using below. Perhaps I am missing an error in my code?

function checkSubscription(){
let user = wixUsers.currentUser;

user.getPricingPlans()
.then( (pricingPlans) => {
let firstPlan = pricingPlans[ 0 ];
let secondPlan = pricingPlans[ 1 ];
let planName1 = firstPlan.name; // “Gold”
let planName2 = secondPlan.name; // “Gold”
let startDate = firstPlan.startDate; // Wed Aug 29 2018 09:39:41 GMT-0500 (Eastern Standard Time)
let expiryDate = firstPlan.expiryDate; // Thu Nov 29 2018 08:39:41 GMT-0400 (Eastern Daylight Time)
console.log(pricingPlans); // Thu Nov 29 2018 08:39:41 GMT-0400 (Eastern Daylight Time)
console.log(planName1 + planName2);
console.log(pricingPlans);

if (planName1 === “TRADIE PLUS” ) {

$w( "#btnMenuTradieProfile" ).expand(); 
$w( "#btnMenuMembersbips" ).collapse(); 
$w( "#btnMenuQuote" ).collapse(); 

     } 

if (planName2 === “TRADIE PLUS” ) {

$w( "#btnMenuTradieProfile" ).expand(); 
$w( "#btnMenuMembersbips" ).collapse(); 
$w( "#btnMenuQuote" ).collapse(); 
     } 

if (planName1 === “TRADIE” ) {

$w( "#btnMenuTradieProfile" ).expand(); 
$w( "#btnMenuMembersbips" ).collapse(); 
$w( "#btnMenuQuote" ).collapse(); 
     } 

if (planName2 === “TRADIE” ) {

$w( "#btnMenuTradieProfile" ).expand(); 
$w( "#btnMenuMembersbips" ).collapse(); 
$w( "#btnMenuQuote" ).collapse(); 
     } 

if (planName1 === “BUSINESS” ) {

$w( "#btnMenuBusinessProfile" ).expand(); 
$w( "#btnMenuMembersbips" ).collapse(); 
$w( "#btnMenuQuote" ).collapse(); 
     } 

if (planName2 === “BUSINESS” ) {

$w( "#btnMenuBusinessProfile" ).expand(); 
$w( "#btnMenuMembersbips" ).collapse(); 
$w( "#btnMenuQuote" ).collapse(); 
     } 

});
}

Interesting. I tried this on a test site and I didn’t get the error. Not sure what else you have in your code.

Please post the editor URL of your site. Only authorized Wix personnel can get access to your site in the editor. Please include the name of the page involved.

It is affecting all pages as the code is on all pages

I don’t get the error as you can see here:

However, I do see numerous errors that you need to correct. Perhaps these errors are causing your problem.

As you can see in this screenshot, you are invoking checkSubscription incorrectly.


You should have this instead:

setTimeout(checkSubscription, 50);

You have another error in the btnlogin_click() event handler:


The wixUsers.logout() function does not return a Promise, so .then() is flagged as an error.

You will need to correct these errors first, and then was can address the original problem.

If you in fact have this code on all pages, you might consider putting this code in the masterPage.js file.

Thank you for taking the time to look at this for me.

Thank you for the suggestion to use the masterPage.js, I thought that was only for header items and did not click that I could use it for elements and functions that are required on all pages. I will utilise this for my universal functions. Can you advise if masterPage.js would work for events, as in my experience I would think not considering you must initiate the event for each element item on each separate page.

Re: btnlogin_click() event handler, I don’t know how to make the wixUsers.logout() function return a promise, I have written this code by replicating what I have seen others do, so if you could point me towards a tutorial about how to fix this I would be extremely grateful.

Sorry Yisreal, I forgot to mention that these errors do not show on my version of the editor, have I done something for this to be so?

You can’t make logout() return a Promise . It just doesn’t. See the logout() API for details.

See the article I want some code to run on every page .

@yisrael-wix ok, so I need to take the .then out of the sequence to remove the error? Apologies for my ignorance.

Hi Yisrael, I have changed the code and the problem has moved to the masterPage.js with the paidPlan query I moved.

I have also removed the .then sequence after the logout function

There are no errors showing on my editor. But that’s not to say there aren’t any.