【问题标题】:React gives me the error TypeError: XXX.map is not a function [duplicate]React 给我错误 TypeError: XXX.map is not a function [重复]
【发布时间】:2020-04-24 08:02:31
【问题描述】:

我是 React 新手,它给了我下面的错误,我无法解决。我在网上搜索,很多人都遇到过同样的问题,但是“地图”是一个内置函数,为什么说它不是一个函数?

import React, { Component } from 'react';

export class UrunlerComponentList extends Component {
    displayName = UrunlerComponentList.name

    constructor(props) {
        super(props);
        this.state = { urunler_: [], loading: true };

        fetch('http://localhost:55992/api/search/arama?q=&PageIndex=1')
            .then(response => response.json())
            .then(data => {
                this.setState({ urunler_: data, loading: false });
            });
    }

    static renderUrunlerTable(urunler_) {
        return (
            <table className='table'>
                <thead>
                    <tr>
                        <th>SKU</th>
                        <th>Name</th>
                    </tr>
                </thead>
                <tbody>
                    {urunler_.map(urun =>
                        <tr>
                            <td>{urun.SKU}</td>
                            <td>{urun.Name}</td>
                        </tr>
                    )}
                </tbody>
            </table>
        );
    }

    render() {
        let contents = this.state.loading
            ? <p><em>Loading...</em></p>
            : UrunlerComponentList.renderUrunlerTable(this.state.urunler_);

        return (
            <div>
                <h1>Urunler</h1>
                <p>This component demonstrates fetching data from the server.</p>
                {contents}
            </div>
        );
    }
}

我调用的 webapi 返回下面的值...

{
    "$id": "1",
    "RecordCount": 879,
    "products": [
        {
            "$id": "2",
            "Id": 17034,
            "BrandId": 3,
            "SKU": "7436B003-1082",
            "Name": "xxxxxxxx",
            "InStock": 1,
            "URL": "xxxxxxxxxx",
            "Description": "xxxxxxxxxxxxxxxxx",
            "Price": 9.90,
            "DiscountRatio": 0.00,
            "TechnicalDetail": "xxxxxxxxxxxxxxxxx",
            "ImageUrl": "7436Bxxxxxx.jpg",
            "Active": true,
            "CreatedDate": "2019-07-20T11:36:35.333",
            "Brand": null,
            "CartDetails": [],
            "OrderDetails": [],
            "ProductCategoryRelations": [],
            "ProductColorRelations": [],
            "ProductFileRelations": [],
            "ProductPropertyRelations": []
        },

【问题讨论】:

  • 尝试在renderUrunlerTable方法中下断点或检查urunler_的值,验证是否为数组?如果是数组,也请注明值。
  • 你的数据不是一个数组而是一个对象

标签: javascript reactjs


【解决方案1】:

确保从您的 fetch 调用返回的值是 array 而不是 undefined,因为您正在将该值设置为您的状态。

fetch('http://localhost:55992/api/search/arama?q=&PageIndex=1')
            .then(response => response.json())
            .then(data => {
                // make sure 'data' is an array
                this.setState({ urunler_: data, loading: false });
            });

【讨论】:

  • 我更新了我的问题
  • @ArifYILMAZ 您是否对所有试图帮助您回答问题的人投了反对票?
  • @TravisJames 我不认为他投了反对票,我想我们通过回答重复的问题让一些人生气了:(
  • 啊,我明白了。哦,我想哈哈
  • 我没有对任何人投反对票。有些人不喜欢我的问题 :) 好吧,如果我用其他答案解决了我的问题,我就不会创建这张票。无论如何,谢谢大家。
【解决方案2】:
           <tbody>
                {typeof urunler_ !== 'undefined' && urunler_.length > 0 ? 
               (urunler_.map(urun =>
                    <tr>
                        <td>{urun.SKU}</td>
                        <td>{urun.Name}</td>
                    </tr>
                )):(<tr><</tr>)}
            </tbody>

【讨论】:

    【解决方案3】:

    这应该可以解决它。数据本身是一个对象而不是数组。我猜你想分配产品。

    ...
    .then(({products}) => this.setState({ urunler_: products, loading: false }))
    ...
    

    【讨论】:

    • 当它说的与接受的答案相同时,它是如何被否决的?由于对象解构,它甚至更好。
    • 我的也被否决了,尽管它说的基本相同。不知道为什么...
    猜你喜欢
    • 2023-02-17
    • 1970-01-01
    • 2019-04-05
    • 2015-05-04
    • 2018-07-23
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2020-05-27
    相关资源
    最近更新 更多