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();
}
});
}