NodeJS Axios响应拦截器始终返回Undefined

vltsax25  于 2023-11-17  发布在  Node.js
关注(0)|答案(1)|浏览(145)

apiCaller一个Axios示例,它使用函数processResponse来处理响应。它将对应的对象发送到前端。
但是当我尝试在前端访问响应时,它总是显示响应是undefined。响应在processResponse函数中正确打印,但不能从前端API调用访问。
processResponse函数是:

const processResponse=(res) => {
    console.log(res.status);
    if (res.status===200) {
        return {isSuccess: true, data: res.data};
    }
    else {
        return {
            isFailure: true,
            status: res?.status,
            msg: res?.msg,
            code: res?.code
        };
    }
};

字符串
Axios响应拦截器是:

apiCaller.interceptors.response.use(
    function (res) {
        return processResponse(res);
    },
    function (err) {
        return Promise.reject(processError(err));
    }
);


它正确地返回被拒绝的Promise,但不返回响应。
来自前端的调用:

const signupUser=async () => {
        const tmp={
            username: username,
            name: name,
            password: password,
        };
        let res=await API.usersignup(tmp);
        console.log(res);
        if (res.isSuccess) {
            setName("");
            setUsername("");
            setPassword("");
            changeLogin;
        }
        else {
            setErr("Oops! Something went wrong :(");
        }
    };


这是它总是显示res是undefined的地方。

isr3a4wc

isr3a4wc1#

您使用Axios示例创建API的方法是不正确的。我也面临着同样的问题,花了7天时间才发现整个问题是使用括号,让我向您展示我的错误代码

for (const [key, value] of Object.entries(SERVICE_URLS)) {
        API[key] = (body, showUploadProgress, showDownloadProgress) => {
                axiosInstance({
                        method: value.method,
                        url: value.url,
                        data:body,
                        responseType: value.responseType,
                        onUploadProgress: function (progressEvent) {
                                if (showUploadProgress) {
                                        let percentageCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
                                        showUploadProgress(percentageCompleted)
                                }
                        }, onDownloadProgress: function (progressEvent) {
                                if (showUploadProgress) {
                                        let percentageCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
                                        showDownloadProgress(percentageCompleted)
                                }
                        }

                })
        }
}

正确的代码是

for (const [key, value] of Object.entries(SERVICE_URLS)) {
        API[key] = (body, showUploadProgress, showDownloadProgress) => 
                axiosInstance({
                        method: value.method,
                        url: value.url,
                        data:body,
                        responseType: value.responseType,
                        onUploadProgress: function (progressEvent) {
                                if (showUploadProgress) {
                                        let percentageCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
                                        showUploadProgress(percentageCompleted)
                                }
                        }, onDownloadProgress: function (progressEvent) {
                                if (showUploadProgress) {
                                        let percentageCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
                                        showDownloadProgress(percentageCompleted)
                                }
                        }

                })
        
}


在第2行,我使用了一个箭头函数,而不是箭头函数

相关问题