A
A
Alexander2021-11-01 20:08:54
typescript
Alexander, 2021-11-01 20:08:54

Axios config object is possibly 'undefined'?

61801f23463ff801258335.png

const $api = axios.create({
  withCredentials: true,
  baseURL: API_URL,
})

$api.interceptors.request.use((config:AxiosRequestConfig) => {
  config.headers.Authorization = `Bearer ${localStorage.getItem('token')}`
  return config
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-11-01
@bingo347

(config.headers ??= {}).Authorization = `Bearer ${localStorage.getItem('token')}`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question