Hey guys, I’m trying to calculate prices based on the location of users who visit so they see them in their local currency. I’m pulling in FX rates from an external API request using fetch (see from import {fetch} part, I attached full code for clarity). I’m facing two problems:
- when I run the code, it produces the following error in the console
typeError: Cannot read properties of undefined (reading ‘GBP’)
This looks it may be a parsing error or authentication error due to CORS?
2. I have selection tags that filter the repeater (first part of the code) but because I pass on the filtered dataset data to the repeater I need to be able to convert the currencies after the selection tags are selected.
Please note I haven’t added the code to identify the country of the user. I do have the code that pulls IP address and gives the country code which I will add later on once the following works.
It’d be great if anyone has any inputs as to how to get this right. Appreciate the help.
import wixData from ‘wix-data’ ;
const databaseName = ‘IVFPackagesbyCountry’ ;
const databaseField = ‘tags’ ;
$w . onReady ( function () {
dataset1_ready ();
$w ( ‘#treatmentTags’ ). onChange (( event ) => {
const selectedTag = $w ( ‘#treatmentTags’ ). value ;
addItemstoRepeater ( selectedTag );
})
});
function addItemstoRepeater ( selectedOption = ) {
let dataQuery = wixData . query ( databaseName );
if ( selectedOption . length > 0 ) {
dataQuery = dataQuery . hasSome ( databaseField , selectedOption );
}
dataQuery
. find ()
. then ( results => {
const filtereditemsReady = results . items ;
$w ( '#packages' ). data = filtereditemsReady ;
})
}
import { fetch } from ‘wix-fetch’ ;
const url = “https://currency-converter5.p.rapidapi.com/currency/convert” ;
export function dataset1_ready () {
$w ( “#packages” ). forEachItem ( ( $item , itemData , index ) => {
let initialAmount = Number ( $item ( “#text25” ). text . split ( ‘$’ )[ 1 ][ 0 ]);
let sourceSymbol = ‘USD’ ;
let targetSymbol = ‘GBP’ ;
let fullUrl = ${ url } ?format=json&from= ${ sourceSymbol } &to= ${ targetSymbol } &amount=1
;
fetch ( fullUrl , {
"method" : "GET" ,
"headers" : {
"x-rapidapi-host" : "currency-converter5.p.rapidapi.com" ,
"x-rapidapi-key" : getSecret ( "Currency_Converter_API_Key" )
}
})
. then ( response => {
// $item("#text25").text = String(initialAmount * 1.3);
$item ( "#text25" ). text = String ( initialAmount * response [ 'rates' ][ targetSymbol ][ 'rate' ]);
console . log ( response );
})
. catch ( err => {
console . error ( err );
});
});
}
// import {getSecret} from ‘wix-secrets-backend’;
export async function getSecret ( key ) {
const mySecret = await getSecret ( key );
return mySecret
}