I get an error on import currency calculator...(1. import {getJSON} from 'wix-fetch';) parsing error: unexpected token import-plz help

  1. import {getJSON} from ‘wix-fetch’;
  2. const url = “https://api.exchangeratesapi.io/latest”;
  3. let currencyOptions = [
  4. { “value”: “USD”, “label”: “US Dollars”},
  5. { “value”: “EUR”, “label”: “Euros”},
  6. “value”: “JPY”, “label”: “Japanese Yen”},
  7. ];
  8. $w.onReady( function () {
  9. populateDropdowns();
  10. $w(‘#calculateButton’).onClick((event) => {
  11. calculateCurrency();
  12. })
  13. });
  14. function populateDropdowns(){
  15. $w(‘Dropdown’).options = currencyOptions;
  16. $w(‘Dropdown’).selectedIndex = 0;
  17. }
  18. function calculateCurrency() {
  19. let initialAmount = $w(“#sourceAmount”).value;
  20. let sourceSymbol = $w(“#sourceCurrency”).value;
  21. let targetSymbol = $w(“#targetCurrency”).value;
  22. let fullUrl = ${url}?base=${sourceSymbol}&symbols=${targetSymbol};
  23. getJSON(fullUrl)
  24. .then(json => {
  25.  $w("#targetAmount").value = initialAmount * json.rates[targetSymbol]; 
    

32.)}

any help?

First off, don’t post a new reply asking for any help only 5 mins after posting your original post or that will put some people off helping as it might sound like you are demanding.

Secondly, please put your code in the code block function using the icon when selecting the text and don’t list it with numbers etc.

As for the issue itself with your code, when it mentions parsing error: unexpected token, that will mean that you should check your code and make sure that you have equal numbers of opening and closing parentheses and curly brackets in your code.

With the parsing error: unexpected token import error, it will most often mean that the error is in your page code and not your backend file.

So, in simple terms, you need to make sure that you have matching pairs of open and closed parentheses which are the ( and the ), as well as matching pairs of curly brackets which are the { and the }.

Going through your code you are missing the opening curly bracket on line 8 and another curly bracket after line 31 and before line 32.

Also, you probably don’t need the end comma ‘,’ at the end of line 8 too as that is the end of the values in the array. Try it without, however if you get the red icon on that line then simply add it back if needed.

So it should be like this.

 
import {getJSON} from 'wix-fetch';

const url = "https://api.exchangeratesapi.io/latest";

let currencyOptions = [
   { "value": "USD",  "label": "US Dollars" },
   { "value": "EUR",  "label": "Euros" },
   { "value": "JPY",  "label": "Japanese Yen" }
 ];
 
 $w.onReady(function() {
  populateDropdowns();
 
 $w('#calculateButton').onClick((event) => {
 calculateCurrency();
 })
 });
 
 function populateDropdowns() {
 $w('Dropdown').options = currencyOptions;
 $w('Dropdown').selectedIndex = 0;
 }
 
 function calculateCurrency() {
 let initialAmount = $w("#sourceAmount").value;
 let sourceSymbol = $w("#sourceCurrency").value;
 let targetSymbol = $w("#targetCurrency").value;
 let fullUrl = `${url}?base=${sourceSymbol}&symbols=${targetSymbol}`;
 getJSON(fullUrl)
 .then(json => {
 $w("#targetAmount").value = initialAmount * json.rates[targetSymbol];
 }
 )}

So basically you have just copied the code from this tutorial on this getting started page.
https://www.wix.com/corvid/reference/GettingStarted.html

However, the separate code sections broken down are correct, although when they put the code together in the final example block it is missing those extra parsing tokens as shown above.

This needs to be fixed asap and one good point from this is that you have exposed an error in the code on that Wix page.

@yisrael-wix
@brett-haralson
Can this be looked at as soon as possible and fixed.

Thanks - I will handle.

I must say thank you first, this worked for me. Secondly this is my first time here, and i am very happy to work with Corvid, i had tried working on it but i got errors, this is my third time and i said why don’t i try community help. corvid has a perfect community, instant help. So excuse me for following up asfter 5 minutes,just wanted to see if anyone cares here. Thanks

@brett-haralson

Thanks for that Brett.

Being picky now too, but can we keep the example code uniform with the code from the API reference too.

On that tutorial it has an extra comma at the end of the options in the dropdown array.

8. // Define the currency option values and text for the dropdowns.
9. let currencyOptions = [
10. { "value": "USD", "label": "US Dollars"},
11. { "value": "EUR", "label": "Euros"},
12. { "value": "JPY", "label": "Japanese Yen"},
13. ]; 

Whereas the actual API reference for dropdown options shows it correctly without the extra comma at the end of the options in the array.

Set the list of options for a dropdown

$w("#myDropdown").options = [
  {"label": "Who's on first!", "value": "first"},
  {"label": "What's on second", "value": "second"},
  {"label": "I Don't Know is on third", "value": "third"}
];

Like I said it is just me being ever so picky here, however it should really match the API reference shouldn’t it. :wink:

@sirvansmo

Glad it works all fine for you now, plus thanks for the vote of confidence for the forum itself, I am sure that all the Wix Admins and Mods will appreciate that. :+1::beers:

Just note though, that you won’t always get help so quick on other times that you post an issue, it will all depend on whether the Wix Admins/Mods are on to answer or any of us Forum Ninjas are on in our free time to help try to answer for them.

Otherwise, you might have to wait a few hours or possibly the next day for a reply back to your issue, plus also remember time zone differences too as this forum is open worldwide so you may post in the morning expecting a reply the same day, however it could be evening for Admins/Mods or some of us Ninjas.

@givemeawhisky This comma “thing” drives me crazy. Although the “correct” way is without that last comma, it always seems to work even with a comma. As far as Wix is concerned, it’s apparently a documentation issue - for the Javascript world it’s an infuriating (for me) inconsistency. Although it’s nothing that a Guinness or two can’t fix.

@yisrael-wix
So, technically the correct way and the wrong way both work, that doesn’t surprise me as most times there are always more than one way to do things.

Although as you said it would be nice to have consistency throughout and not inconsistency with everything, so at least all users are working to the one way only and not doing one thing somewhere and then being told to do it differently somewhere else.

Make sure that your Guinness has an shot of Whisky added to it, makes it so much better.
Two drinks in one glass for you, just as long as it isn’t your round to pay :wink:

@givemeawhisky The “correct” way is not to add anything to Guinness - I drink the “black stuff” unadulterated. As for whisky, I don’t even add water - add it to beer? Just thinking about it gives me the heebie jeebies. I’m a purist.

@yisrael-wix
Move with the times, Guinness isn’t just the black stuff anymore, although it is if you stick with the one and only original. :wink: https://www.guinness.com/en-gb/our-beers/

I thought the same as you did too with adding it too, however Brother-in-law was adding it to my drinks on my wedding day/night, so I got used to the taste of it. Plus, I wasn’t paying for the drinks so a win, win for me, two drinks in one and all for free :grin: