Need help please - Parsing error: unexpected token

I am at the point of tearing my hair out with this. I feel it may be simple but I can’t fix this issue, any help would be amazing!

I’m getting the unexpected token parsing error in the code shown below…

it states the “unexpected token ‘}’” but when I remove it from the code, the error signal just jumps to the bottom of the code on a blank line (or whatever the very last line is) and gives no indication of a token anymore, but still says “unexpected token”.

I’ve tried changing a few things but the error signal just keeps jumping around the page…

I think you have an extra “)” in line 38, instead of
});
Please try
}

And line 37 should be }); not }

Thank you, I’ve corrected both of those issues. The error message has now jumped to the bottom line (46 - nothing on that line, it’s the bottom line of the code) and says unexpected token but not indicating a token?

Sorry, I can only stroll the code down to line 44, there is no 45 and 46. From what is in line 44, it seems it is the same mistake. L ine 44 should be }); not }

Sorry, I hadn’t captured the end bit. Here is the bottom of the page

Line 44 should be }); not }

Okay done that, still the same thing happening. Sorry! I’ve only just started having a go at using corvid so not the best at this stuff atm.

Okay so I’ve just added " } ) " to close the on ready function brackets as I noticed they weren’t complete. And the error has gone!

But now the thing that I was actually trying to achieve isn’t working hhaha.

I wanted to make a section collapse and then when a user clicks a button (button4) the section expands - BUT only if their user role is “Artist”.

I was then going to add a differnt collapsed section that would expand if the user role was different.

I’ve pasted the code I used to try and achieve this below…

let user = wixUsers.currentUser;

let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true

user.getEmail()
  .then( (email) => {
 let userEmail = email;      // "user@something.com"
  } );

user.getRoles()
  .then( (roles) => {
 let firstRole = roles[0];
 let roleName = firstRole.name;                // "Role Name"
 let roleDescription = firstRole.description;  // "Role Description"
  } );

user.getPricingPlans()
  .then( (pricingPlans) => {
 let firstPlan = pricingPlans[0];
 let planName = firstPlan.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)
  } );

if (user.roleName === 'Artist' && user.loggedIn) {
  $w("#button4").onClick( (event) => {
  $w("#section13").expand();
  });
}

@lisamthorpe Based on your code, you only installed the event handler when the user is artist and is logged in, since the code runs only once, if any condition is false, there will be no handler, so section13 will not expand when clicking button4. And once it is expanded, it stayed in expanded mode, I don’t really understand your logic behind all these. Anyway, you should be able to troubleshoot the problem by adding console log messages, e.g. console.log(something ) in the handler to verify the code ’ $w ( “#section13” ). expand ()’ is executed by monitoring if ‘something’ is shown in the console log in preview mode.

It looks like you have just taken the code sample from Wix Users API and the currentUser function.
https://www.wix.com/corvid/reference/wix-users.html#currentUser

Examples
Get the current user’s information

import wixUsers from 'wix-users';

// ...

let user = wixUsers.currentUser;

let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true

user.getEmail()
  .then( (email) => {
    let userEmail = email;      // "user@something.com"
  } );

user.getRoles()
  .then( (roles) => {
    let firstRole = roles[0];
    let roleName = firstRole.name;                // "Role Name"
    let roleDescription = firstRole.description;  // "Role Description"
  } );

user.getPricingPlans()
  .then( (pricingPlans) => {
    let firstPlan = pricingPlans[0];
    let planName = firstPlan.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)
  } );

So where you have a parsing error, you simply have either too many or not enough of the parentheses or the curly brackets in your code.

There should be matching pairs, so equal numbers of opening and closing parentheses which are the ‘(’ and ‘)’.

Along with equal numbers of opening and closing curly brackets, which are the ‘{’ and ‘}’.

So basically, you would just have to scan through your used code and make sure that there are the same number of opening and closing for both of them.

If you use a search engine you can find many pages telling you all about the functions of parentheses and curly brackets etc, like these below and many more.
https://zellwk.com/blog/es6/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Object_literals

Also, note that in the code sample, where the ‘// …’ is in the code, you need to add a onReady function for that page itself.
https://www.wix.com/corvid/reference/$w.html#onReady

So for a take on your code for the button to do something if the user is that role and logged in etc, you can do as like below.

You would need to have both of the sections used for user or not user parts set as collapsed on load in the properties panel for those section elements.

So if the logged in user is the right one, then that section will expand, otherwise for anybody else another section will expand instead.

Now obviously you can take this and do whatever you want with it for the else part, you can simply have them redirected to another page if they are not the right person.

Also note that I have changed the button to be above the if user line so that it will check that line in the onClick event itself.

I have also changed it to an export function instead, so you will need to add an onClick event to the button through the properties panel for that button and then delete the current line with the added line from adding the onClick event.

import wixUsers from 'wix-users';
	
$w.onReady(function () {
});

let user = wixUsers.currentUser;

let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true

user.getEmail()
  .then( (email) => {
    let userEmail = email;      // "user@something.com"
  } );

user.getRoles()
  .then( (roles) => {
    let firstRole = roles[0];
    let roleName = firstRole.name;                // "Role Name"
    let roleDescription = firstRole.description;  // "Role Description"
  } );

user.getPricingPlans()
  .then( (pricingPlans) => {
    let firstPlan = pricingPlans[0];
    let planName = firstPlan.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)
  } );

  export function button4_click(event) {
  if (user.roleName === 'Artist' && user.loggedIn) {
  $w("#section13").expand();
  } else {
  $w("#section14").expand();
  }
  }

Finally, please take note of the Wix Users API info and only test it on a published site, as it won’t fully work on a preview test only.

Note
The APIs in wix-users are only partially functional when previewing your site. View a published version of your site to see their complete functionality.

The APIs in wix-users can only be used once the page has loaded. Therefore, you must use them in code that is contained in or is called from the onReady() event handler or any element event handler.

@givemeawhisky You are a saint. Thank you so much for your help!

If you don’t mind me asking… As I am new to using corvid (or attempting to), is there any way you would recommend to learn how to use the APIs etc. properly? I have been trying to figure out if there is anything that actually details HOW to use it as a whole if you’re new to it but I can’t seem to find anything. Or is that you actually do need an understanding of coding first?

Otherwise, I am just stuck with trying to find snippets of code and APIs to do the things I want as I go but not actually have an understanding of how to add it to my site etc. Which obviously takes quite a while and I never seem to be able to actually understand how it works from that, so therefore I can’t apply any knowledge on the next funciton I wan’t to create.

I hope that question makes sense haha!

@givemeawhisky Okay I may have spoken to soon… I thought it was working because section14 was expanding in preview but when I tested by logging in as an ‘Artist’ I still only got section14 expanding… So it seems that etiher section13 expanding or identifying the user role is not working…

If all that you are wanting to do is to check if the user is logged in and are a specific role, then just do something like this below.

So if the user is not logged in or Admin it will show section 14, however if the user is logged in and Admin role, then it will show the button 4 which would then simply expand section 13 as before.

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';

$w.onReady(function(){
    const currentUser = wixUsers.currentUser;
    if (currentUser.loggedIn && currentUser.role === 'Admin'){
    $w('#button4').show();
    } else {
    $w('#section14').expand();
    }
    });

wixUsers.onLogin( (user) => {
  wixLocation.to(wixLocation.url);
});
  
export function button4_click(event) {
$w("#section13").expand();
}

Or something like this.

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';

$w.onReady(function () {
	$w("#section14").expand();
	let user = wixUsers.currentUser;
	if (user.loggedIn) setupPage();
	wixUsers.onLogin(() => {
		setupPage();
	});
});

function setupPage() {
	let user = wixUsers.currentUser;
	user.getRoles()
		.then((roles) => {
			const currentUser = wixUsers.currentUser;
			if (currentUser.loggedIn && currentUser.role === 'Admin') {
				$w('#button4').show();
        $w('#section14').collapse();
			}
		});
}

export function button4_click(event) {
	$w("#section13").expand();
}

To find out more about Wix Corvid, then have a read of the Wix Support pages about it to begin with.
https://support.wix.com/en/corvid-by-wix/basics

Also, you will find lots of code examples and detailed info about the Wix API in the Wix API Reference.
https://www.wix.com/corvid/reference

Wix Corvid Examples that you can open fully made up in your own Wix Editor.
https://www.wix.com/corvid/examples

Wix Video Tutorials.
https://www.wix.com/corvid/video-tutorials

You can also view Wix YouTube videos.
https://www.youtube.com/user/Wix

Plus, also searching this Corvid Forum for posts on a subject that you are looking to do.

Finally, there is always your own search engine in your internet browser, that can often find a lot of things :wink:

Also, sign up to Wix sites from the likes of…

Nayeli (Code Queen)
https://www.totallycodable.com/
https://www.youtube.com/channel/UCy4ydlG0gU7cTUBDYTD2I_A

Michael (WTA)
https://www.wixtrainingacademy.com/wix-design-pro-michael-strauch
https://www.youtube.com/user/ComputerMDofGilbert/featured?disable_polymer=1