[英]How to access Google Cloud Run Environment Variables from React App
I have Cloud Run environment variables as defined for my Google Cloud Run React App deployment.我有为我的 Google Cloud Run React App 部署定义的 Cloud Run 环境变量。
However when I try to access them inside the react app as below, it gets "undefined" values.但是,当我尝试在下面的反应应用程序中访问它们时,它会得到“未定义”的值。
export const BASE_FRONTEND_URL = process.env.REACT_APP_BASE_FRONTEND_URL
export const BASE_BACKEND_URL = process.env.REACT_APP_BASE_BACKEND_URL
I understand that the values above are in client browser, and there are no variables exist at that moment.我知道上面的值是在客户端浏览器中,当时不存在任何变量。
But how should I get the environment variables from Google Cloud Run dashboard and propagate to the client without relying on ".env" files.但是我应该如何从 Google Cloud Run 仪表板获取环境变量并传播到客户端而不依赖“.env”文件。
If you want to use those variables (process.env.REACT_APP_BASE_FRONTEND_URL, process.env.REACT_APP_BASE_BACKEND_URL) into your react app then you have to create a .env file, which will contain all environment variables for your react app because the library which provide this named dotenv have default configuration to get all those environment variables from .env file.如果你想在你的 react 应用程序中使用这些变量(process.env.REACT_APP_BASE_FRONTEND_URL、process.env.REACT_APP_BASE_BACKEND_URL),那么你必须创建一个 .env 文件,该文件将包含你的 react 应用程序的所有环境变量,因为提供这个的库名为dotenv的默认配置可以从 .env 文件中获取所有这些环境变量。
So you have to get those env variables from Google cloud and paste them into .env file, then the issue should be solved.所以你必须从谷歌云中获取这些环境变量并将它们粘贴到 .env 文件中,然后问题应该得到解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.