process.env.ENV_VARIABLE is undefined
eakenbor opened this issue · comments
eakenbor commented
Contact Details
No response
What happened?
I have a function in my server code that relies on process.env.GOOGLE_MAPS_API_KEY. Whenever I call the function within my server codes, process.env.GOOGLE_MAPS_API_KEY is read. But, when I call it inside an Adminjs component, it appears as undefined. Please how can I solve this problem, please?
import axios from "axios"
const getAddressByLatLng = async (lat, lng) => {
const addressObj = await axios.post(
`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&sensor=true&key=${process.env.GOOGLE_MAPS_API_KEY}`)
if (!addressObj) {
throw new Error("Location not found!")
}
const addressComponents = addressObj.data.results[0].address_components
const location = {}
const extractLocation = (type) => {
const obj = addressComponents.find(e => e.types.includes(type))
if (obj) {
return obj
} else {
return {}
}
}
location.countryName = extractLocation("country").long_name
location.country = extractLocation("country").short_name
location.state = extractLocation("administrative_area_level_1").long_name
if (!extractLocation("locality").long_name) {
location.city = extractLocation("administrative_area_level_2").long_name
} else {
location.city = extractLocation("locality").long_name
}
location.street = extractLocation("route").long_name
location.zipCode = extractLocation("postal_code").long_name
location.streetNumber = extractLocation("street_number").long_name
location.formattedAddress = addressObj.data.results[0].formatted_address
location.geoCoordinates = {
lat,
lng
}
return location
}
export default getAddressByLatLng
Bug prevalence
Every time
AdminJS dependencies version
"adminjs": "^7.0.5"
What browsers do you see the problem on?
Chrome
Relevant log output
No response
Relevant code that's giving you issues
No response
Rafał Dzięgielewski commented
You'd have to define envs when creating AdminJS instance:
new AdminJS({
env: {
GOOGLE_MAPS_API_KEY: 'xxx',
}
})
and you'll be able to access it via window.AdminJS.env.GOOGLE_MAPS_API_KEY