【问题标题】:Axios response return object instead of arrayaxios 响应返回对象而不是数组
【发布时间】:2019-12-19 14:08:09
【问题描述】:

我在本机反应中使用 axios。 postman 中的原始响应如下所示:

{
    "id": 2,
    "parent_id": 1,
    "name": "Default Category",
    "is_active": true,
    "position": 1,
    "level": 1,
    "product_count": 2,
    "children_data": [
        {
            "id": 3,
            "parent_id": 2,
            "name": "Papers",
            "is_active": true,
            "position": 1,
            "level": 2,
            "product_count": 2,
            "children_data": [
                {
                    "id": 5,
                    "parent_id": 3,
                    "name": "A44",
                    "is_active": true,
                    "position": 1,
                    "level": 3,
                    "product_count": 0,
                    "children_data": []
                }
            ]
        },
        {
            "id": 6,
            "parent_id": 2,
            "name": "Laptop",
            "is_active": true,
            "position": 2,
            "level": 2,
            "product_count": 1,
            "children_data": []
        }
    ]
}

当我尝试这个时如何console.log(typeof categoryResponse.data.children_data); 我得到对象。这会导致问题,因为我正在尝试更新类型数组的 react-native 中的状态变量。甚至打印 console.log(categoryResponse.data); 时的响应对象看起来也很奇怪。

对象{“children_data”:数组[ 目的 { “children_data”:数组 [ 目的 { "children_data": 数组 [], “身份证”:5, “is_active”:是的, “级别”:3, “名称”:“A44”, “父ID”:3, “位置”:1, "product_count": 0, }, ], “身份证”:3, “is_active”:是的, “级别”:2, “名称”:“论文”, “父ID”:2, “位置”:1, “product_count”:2, }, 目的 { "children_data": 数组 [], “身份证”:6, “is_active”:是的, “级别”:2, “名称”:“笔记本电脑”, “父ID”:2, “位置”:2, “product_count”:1, }, ], "id": 2, "is_active": true, "level": 1, "name": "Default Category", "parent_id": 1, "position": 1,
"product_count": 2, }

请帮忙

【问题讨论】:

    标签: react-native axios


    【解决方案1】:

    在 javascript 中,typeof 数组实际上是一个对象。在 javascript 中只有 6 种数据类型。数组是对象的子集,因此console.log(typeof categoryResponse.data.children_data) 会返回对象,即使它是一个数组。

    但是你可以看到console.log(categoryResponse.data),你看到了

    Object { "children_data": Array [ Object { "children_data": Array [ Object { "children_data": Array [], "id": 5, "is_active": true, "level": 3, "name": "A44", "parent_id": 3, "position": 1, "product_count": 0, }, ], "id": 3, "is_active": true, "level": 2, "name": "Papers", "parent_id": 2, "position": 1, "product_count": 2, }, Object { "children_data": Array [], "id": 6, "is_active": true, "level": 2, "name": "Laptop", "parent_id": 2, "position": 2, "product_count": 1, }, ], "id": 2, "is_active": true, "level": 1, "name": "Default Category", "parent_id": 1, "position": 1,
    "product_count": 2, }
    

    在这里您可以看到“childern_data”是一个数组。

    "children_data": Array [ Object {
    

    所以基本上它是一个数组,不要让 typeof 混淆你。

    希望对您有所帮助。如有疑问,请清除它

    【讨论】:

    • 那为什么数组还是空的呢? const [类别,setCategories] = useState([]); setCategories(categoryResponse.data.children_data); console.log(categories);
    • 哪里是空的?它在那里吗?只有第三个嵌套的 ""children_data": []" 是空的,根据邮递员的说法
    • "categoryResponse.data.children_data" 这应该是内容数组..对吗?设置钩子变量后,钩子变量在console.log(categories)中仍然显示为空
    • 嗯,但只是 console.log(categoryResponse.data.children_data) ,如果它不为空则意味着 setState 代码有问题,分享它一个博览会小吃链接,将在那里检查
    • 已解决问题..给您带来的不便,敬请见谅
    猜你喜欢
    • 2020-10-06
    • 2021-04-18
    • 2023-03-29
    • 2020-04-08
    • 1970-01-01
    • 2021-05-18
    • 2020-04-11
    • 2018-07-05
    • 1970-01-01
    相关资源
    最近更新 更多