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
});
>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
});