javascript axios.create({MY Conig})引发错误(配置未定义)

bnlyeluc  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(79)

在Windows 10中,我使用react-router-dom 5.2.0和react-redux 7.2.5和react 17.0.2和axios 0.21.4以及WebStorm 2023.1.3 IDE。
axiosInstance.js:

import jwt_decode from 'jwt-decode';
import dayjs from 'dayjs';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { updateAccessToken } from '../actions/userActions';

const baseURL = 'http://127.0.0.1:8000';

export const axiosInstance = (userInfo, dispatch) => {
    const instance = axios.create({
        'mode': 'cors',
        baseURL: baseURL,
        headers:  {
            'Content-Type': 'application/json',
            Authorization: `Bearer ${userInfo?.access}`,
        }
    });

    instance.interceptors.request.use(async (req) => {
            const user = jwt_decode(userInfo.access);
            const isExpired = dayjs.unix(user.exp).diff(dayjs()) < 5000;
            if (!(isExpired)) {
                return req
            }

            const response = await axios.post(
                '/api/v1/users/token/refresh/', { refresh: userInfo.refresh },
            );

            dispatch(updateAccessToken(response.data))
            req.headers.Authorization = `Bearer ${response.data.access}`;
            return req;
    });
    return instance
}

字符串
userActions.js:

export const  getUserDetailsAction = () => async (dispatch, getState) => {
    try {
        dispatch({
            type: USER_DETAILS_REQUEST
        });
        const { userLogin: { userInfo } } = getState();

        const authRequestAxios = axiosInstance(userInfo, dispatch)
        const { data } = await authRequestAxios.get(`/api/v1/users/${userInfo._id}/`);
        dispatch({
            type: USER_DETAILS_SUCCESS,
            payload: data,
        });
        localStorage.setItem('userDetails', JSON.stringify(data));
    } catch (error) {
        dispatch({ // USER FURTHER INFORMATION FAILED
            type: USER_DETAILS_FAILED,
            payload: error.response && error.response.data.detail ? error.response.data.detail : error.message,
        });
    }


错误IDE:
参数类型{mode: string, headers: {Authorization: string, "Content-Type": string}, baseURL: string}不能分配给参数类型 * AxiosRequestConfig| undefined * 对象文字只能指定已知属性,但以下是extra: 'mode'

szqfcxe2

szqfcxe21#

  • 请在从配置对象中删除mode属性后运行代码,因为它不是有效的Axios配置选项,并像这样导入“import axios from 'axios';,import { AxiosRequestConfig } from 'axios';"*

相关问题