I have an issue. In my Host/cloud solution, I must use environment variables of pricing for each country this way 'defined in their "Environment Variables").
BASIC_PRICE_FR_PRODUCT = "50";
COMPLEX_PRICE_FR_PRODUCT = 100;
BASIC_PRICE_UK_PRODUCT = "37";
COMPLEX_PRICE_UK_PRODUCT = "200";
BASIC_PRICE_ES_PRODUCT = "75";
COMPLEX_PRICE_ES_PRODUCT = "300";
I can access those using process.env.XXX such as process.env.BASIC_PRICE_FR
As you see these environment variables depend on the country as the price vary from one country to the other.
In our node.js app, the challenge is that when a function is executed, it is self aware of the country so, we can (and must) use the "current" country and the current country_iso_code ("fr" for example), and with this we must use the pricing that match this country.
After reading on SO some posts on "dynamic variable names" , I tried eval, global[] and window[] like below, but none work and all outputs "undefined" values
//note: iso_code_3166_we_can_use is something passed to the function by the final user or by some other lambda in the function context.
const current_country_iso_code_uppercase = iso_code_3166_we_can_use;
const basicPrice = parseInt( "process.env.BASIC_PRICE_" + current_country_iso_code_uppercase + "_PRODUCT")
console.log(basicPrice)//bug here as outputs "undefined"
EDIT
The suggestion of using process.env['xxx'] did not work so I add here the results
console.log(process.env.BASIC_PRICE_FR_PRODUCT);//outputs 50
console.log('BASIC_PRICE_' + iso_code_uppercase + '_PRODUCT' );//just to be sure :): outputs BASIC_PRICE_FR_PRODUCT
console.log( process.env['BASIC_PRICE_' + iso_code_uppercase + '_PRODUCT'] );// DOES NOT WORK, outputs undefined
Use []
to dynamically access an object's property:
var country = 'FR'
var price = process.env['BASIC_PRICE_' + country + '_PRODUCT']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With