Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Updated global variable not being passed into String

I’m getting this issue where I try to set a global variable inside a function and use a globally declared string with concatenation of the said variable.

When checked inside the method, the variable has been set correctly, but it’s not detected in the concatenated String.
What could be the reasoning behind this.

const axios = require('axios').default;
const express = require('express');

let environment;
let myUrl = `https://ets-tst-${environment}.mydomain.com/health`

app.get('/', function (req, res) {

    environment= 'dev'
    console.log(environment)  //prints dev
    console.log(myUrl) //prints https://ets-tst-undefined.mydomain.com/health
});

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you set environment after myUrl you can’t set value in variable
you can set before this like:

const axios = require('axios').default;
const express = require('express');

let environment = 'dev';
let myUrl = `https://ets-tst-${environment}.mydomain.com/health`

app.get('/', function (req, res) {

    console.log(environment)  //prints dev
    console.log(myUrl) //prints https://ets-tst-undefined.mydomain.com/health
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading