environment variables not working with React and variables [NODE]

时间:2019-03-19 15:18:16

标签: node.js environment-variables

I have a problem with my environment variables. It is like variables doesn't exist.

Here a picture of my problem: Here a picture of my problem

in the .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 :

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 :

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

thank you in advance

1 个答案:

答案 0 :(得分:0)

感谢@ bato3,它现在可以工作了!

我在这里恢复:

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

.env中:

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

谢谢!