12

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
9
  • 1
    Have you tried process.env["BASIC_PRICE_" + current_country_iso_code_uppercase + "_PRODUCT"]? Commented Jan 14, 2019 at 22:31
  • @Xufox i'll try now, no i haven't, i tried some circumvented/complex stuff ...why didn't i think of it? will update if it works Commented Jan 14, 2019 at 22:33
  • @Xufox did not work, see my edits 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 use process.env.PRODUCT_PRICES["BASIC_PRICE_" + current_country_iso_code_uppercase + "_PRODUCT"]? Apparently process.env is a bit… “special”. Commented Jan 14, 2019 at 23:20
  • 1
    UPDATE: looks like there's a webpack fix in progress: github.com/webpack/webpack/pull/8721 Commented Mar 6, 2019 at 20:17

2 Answers 2

21

Use [] to dynamically access an object's property:

var country = 'FR'
var price   = process.env['BASIC_PRICE_' + country + '_PRODUCT']
Sign up to request clarification or add additional context in comments.

1 Comment

tried but did not work. I added what it outputted in my question EDIT
-3
//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'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.