【问题标题】:ReactJS - Replacing a fetch API with axios is not workingReactJS - 用 axios 替换 fetch API 不起作用
【发布时间】:2020-02-29 06:12:10
【问题描述】:

我在带有 FETCH api 的 ReactJS 中有一段代码运行良好,但是当我尝试用 AXIOS 替换它时,即使我检查了文档,它也不能正常运行。

获取 API 的工作代码:

const signup = (user) => {
       return fetch(`${API}/signup`, {
            method: "POST",
            headers: {
                Accept: 'application/json',
                "Content-Type": 'application/json'
            },
            body: JSON.stringify(user)
        })
        .then(response => {
            return response.json();
        })
        .catch(err => {
            console.log(err);
        });
    }

    const clickSubmit = (event) =>{
        event.preventDefault();
        signup({name, email, password})
            .then(data => {
                if(data.error){
                    setValues({...values, error: data.error, success: false})
                }
                else{
                    setValues({...values, name: '', email: '', password: '', error:'', success:true})
                }
            })
    }

不使用相同的代码,但使用 AXIOS 库:

import axios from 'axios';

 const signup = (user) => {
       return axios(`${API}/signup`, {
            method: "POST",
            headers: {
                Accept: 'application/json',
                "Content-Type": 'application/json'
            },
            data: JSON.stringify(user)
        })
        .then(response => {
            return response.data;
        })
        .catch(err => {
            console.log(err);
        });
    }

    const clickSubmit = (event) =>{
        event.preventDefault();
        signup({name, email, password})
            .then(data => {
                if(data.error){
                    setValues({...values, error: data.error, success: false})
                }
                else{
                    setValues({...values, name: '', email: '', password: '', error:'', success:true})
                }
            })
    }

用 axios 库编写上述代码后出现的错误是:

未处理的拒绝(TypeError):无法读取未定义的属性“错误”

使用 axios 的代码有什么问题?

注意:

显然我将其缩小到未定义的地方。

signup({name, email, password})
            .then(data => {
                if(data.error){
                    setValues({...values, error: data.error, success: false})
                }
                else{
                    setValues({...values, name: '', email: '', password: '', error:'', success:true})
                }
            })
    }

.then() 块中的“数据”未定义,我不知道为什么 fetch api 工作正常。

【问题讨论】:

  • 如果你添加它会起作用:return response.data;而不是返回响应;

标签: reactjs axios fetch-api


【解决方案1】:

参考此处找到的 Axios 文档:https://github.com/axios/axios#response-schema

你缺少的是

.then(response => {
    return response.data;
})

代替:

.then(response => {
    return response;
})

【讨论】:

  • 是的...我更新了问题,并在注释中添加了最新的观察结果。后端被完美地ping通。如果我立即使用 fetch 更改代码,代码工作得非常好。
  • 尝试添加调试器;在返回 response.data 之前;你会注意到有些东西不对劲。
  • 我将添加调试器并查看它。
  • 当我将它与 axios 和使用调试器一起使用时,我看到 response.data 未定义。不知道是什么原因造成的。
  • @arjunsah 您在“网络”选项卡中看到了什么?你能看到你的请求被提出并且响应是正确的吗?响应包含什么?
猜你喜欢
  • 2018-10-28
  • 2018-03-01
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 1970-01-01
  • 2020-10-20
相关资源
最近更新 更多