简体   繁体   English

环境变量不能与React和变量一起使用[NODE]

[英]environment variables not working with React and variables [NODE]

I have a problem with my environment variables. 我的环境变量有问题。 It is like variables doesn't exist. 就像变量不存在一样。

Here a picture of my problem: 这是我的问题图片: 这是我的问题的图片

in the .env : .env

REACT_APP_GET_CLIENT_URL=http://localhost:3002/api/clients/${res.data.userId}?access_token=${res.data.id}

and this is where I call the env : 这就是我称之为env的地方:

login: (data) => {
    axios.post(process.env.REACT_APP_LOGIN_URL, data)
    .then((res) => {
       localStorage.setItem('token', res.data.id)
       localStorage.setItem('userId', res.data.userId)
       axios.get(process.env.REACT_APP_GET_CLIENT_URL)
       .then((res) => {
           localStorage.setItem('firstName', res.data.firstName)
           localStorage.setItem('lastName', res.data.lastName)
           localStorage.setItem('picture', res.data.picture)
           localStorage.setItem('namePicture', res.data.namePicture)
           dispatch({type: 'LOGIN'});  
           }) 
       }
       ).catch((err) => {
          console.log(err);
          dispatch({type: 'LOGIN_ERR'});
   })
},

process.env.REACT_APP_LOGIN_URL is working with : process.env.REACT_APP_LOGIN_URL正在使用:

REACT_APP_LOGIN_URL = http://localhost:3002/api/clients/login

thank you in advance 先感谢您

Thanks to @bato3, it is working now ! 感谢@ bato3,它现在正在运行!

I resume here : 我在这里继续:

login: (data) => {
        axios.post(process.env.REACT_APP_LOGIN_URL, data)
          .then((res) => {
            localStorage.setItem('token', res.data.id)
            localStorage.setItem('userId', res.data.userId)
            let REACT_APP_GET_CLIENT_URL = process.env.REACT_APP_GET_CLIENT_URL
              axios.get(REACT_APP_GET_CLIENT_URL.replace(':userId:', res.data.userId).replace(':token:', res.data.id))
              .then((res) => {
              console.log(process.env.REACT_APP_GET_CLIENT_URL)
              localStorage.setItem('firstName', res.data.firstName)
              localStorage.setItem('lastName', res.data.lastName)
              localStorage.setItem('picture', res.data.picture)
              localStorage.setItem('namePicture', res.data.namePicture)
              dispatch({type: 'LOGIN'});  
              }) 
          }
          ).catch((err) => {
            console.log(err);
            dispatch({type: 'LOGIN_ERR'});
        })
        },

in the .env : .env

REACT_APP_GET_CLIENT_URL=http://localhost:3002/api/clients/:userId:?access_token=:token"

Thank you ! 谢谢 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM