I have a problem with my environment variables. It is like variables doesn't exist.
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
答案 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"
谢谢!