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
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
Share
Improve this question
edited Jan 14, 2019 at 23:23
Mathieu
asked Jan 14, 2019 at 22:28
MathieuMathieu
4,78713 gold badges64 silver badges125 bronze badges
9
-
1
Have you tried
process.env["BASIC_PRICE_" + current_country_iso_code_uppercase + "_PRODUCT"]
? – Sebastian Simon Commented Jan 14, 2019 at 22:31 - @Xufox i'll try now, no i haven't, i tried some circumvented/plex stuff ...why didn't i think of it? will update if it works – Mathieu Commented Jan 14, 2019 at 22:33
- @Xufox did not work, see my edits – Mathieu Commented Jan 14, 2019 at 23:14
-
Does it work if you group all six variables in a single object, e.g.
PRODUCT_PRICES
, then useprocess.env.PRODUCT_PRICES["BASIC_PRICE_" + current_country_iso_code_uppercase + "_PRODUCT"]
? Apparentlyprocess.env
is a bit… “special”. – Sebastian Simon Commented Jan 14, 2019 at 23:20 - 1 UPDATE: looks like there's a webpack fix in progress: github./webpack/webpack/pull/8721 – dkershaw Commented Mar 6, 2019 at 20:17
2 Answers
Reset to default 21Use []
to dynamically access an object's property:
var country = 'FR'
var price = process.env['BASIC_PRICE_' + country + '_PRODUCT']
//Create an empty dictionary,
var process_env_dict = {};
process_env_dict['env'] = process.env;
//then you can access it with the below statement as you expected
var result = JSON.parse(JSON.stringify(process_env_dict))['env']['BASIC_PRICE_' + country + '_PRODUCT'];