Hi there! I'm trying to teach myself some javascript to access 3rd party web services. Upon following the support documentation we are presented with 2 forms of doing it:
Client-side Service Call: the example returns the currency exchange rate from USD to a user defined currency. I tried following the steps including the 3 elements on my wix page (text input, button and text label) and then connecting the button event on click with the function "FetchRate" as follows:
$w.onReady(function () {
//TODO: write your page related code here...
});
export function buttonFetchRate_onClick(event) {
//Add your code for this event here:
var url = 'http://api.fixer.io/latest?base=USD';
var symbol = $w("#textInputSymbol").value;
var fullUrl = url + '&symbols=' + symbol;
fetch(fullUrl, {
method: 'get'
})
.then(response => response.json())
.then(json => $w("#textRate").text = json.rates[symbol]);
}
but when previewing the site and clicking the button I'm getting this error:
Wix code SDK error: The text parameter that is passed to the text method cannot be set to the value 0.85259. It must be of type string.
What am I doing wrong?
Thank you Sam. I'll do just that. FYI the same was happening with the backend method to fetch.
Cheers
Hey Felipe,
Looks like you caught a bug. The code should work as is, but the text element's text property is not accepting a value that is a number type. We're on the bug and hopefully will have it fixed soon.
In the meantime, there is a simple workaround. You can convert the number to a string. Just replace the last line before the closing curly brace with this:
.then(json => $w("#textRate").text = json.rates[symbol].toString());