【问题标题】:× TypeError: Cannot read properties of undefined (reading 'map')× TypeError: Cannot read properties of undefined (reading 'map')
【发布时间】:2021-11-03 21:37:33
【问题描述】:

当我尝试运行这段代码时,它给了我这个错误:

× TypeError: Cannot read properties of undefined (reading 'map')

我不知道为什么。我尝试了可能的方法,但没有奏效。

    import React from 'react';
    import Grid from '@material-ui/core/Grid';

    import Product from './Product/Product';
    import useStyles from './styles';

    const products = [
      {id: 1, name: 'Shoes', description: 'Running Shoes.' },
      {id: 2, name: 'MacBook', description: 'Apple MacBook.' },
    ];

    const Products = ({ products }) => {
      const classes = useStyles();

      return (
        <main className={classes.content}>
          <div className={classes.toolbar} />
          <Grid container justify="center" spacing={4}>
            {products.map((products) => (
              <Grid item key={product.id} item xs={12} sm={6} md={4} lg={3}>
                <Product />
              </Grid>
            ))};
          </Grid>
        </main>
      );
    };

    export default Products;

【问题讨论】:

  • 我第一次在 SO 的新措辞中看到这个错误 ^^ 仍然感觉不真实,由于我使用 v8 打开的一个错误,这个超级常见的错误消息被更改了。对于不知道的人:它的含义与Cannot read property 'map' of undefined 完全相同。
  • 消息变化会掩盖多年的错误消息搜索结果。 @CherryDT 您最初发现的有趣错误。
  • @CherryDT 最初的错误是什么?有 GH 问题的链接吗?我很想看看这种变化的历史
  • @CherryDT 很有趣,谢谢!

标签: reactjs material-ui


【解决方案1】:

您的组件中有一个属性“产品”。该变量的优先级高于您在外面的地图,并且您的 .map 正在使用它。我建议重命名其中一个,以避免使用相同名称的变量。

鉴于错误,我猜该属性未传递给组件。

另外,map lambda 的参数也是“products”。把它改成“产品”,否则会失败。

【讨论】:

【解决方案2】:

您传递给组件 (Products) 的属性 products 未定义。 Map 方法考虑了您作为属性传递的products 不是您在组件本身之外创建的。

如果您想映射出您在组件之外创建的 products 数组,那么只需更改其名称,因为该数组与传递的属性具有相同的名称。如果您想使用products(来自属性),请确保您在组件中传递属性。

【讨论】:

    【解决方案3】:

    检查减速器中的 INITIAL_STATE。

    你必须在你在 reducer 中调用的 ID 对象中指定一个数组。

    例子:

    const INTIAL_STATE = {
        products: [],
        product: { name: "", brand_name: "", prices: "", color: "", images: []},
        error: "",
    }
    
    product.images.map(img => {
        return(
            <div key = {img.id} className = "img-itemm">
                <img src = {img.image} alt = {img.image} />
            </div>
        )
    })
    

    【讨论】:

      【解决方案4】:

      确保将产品属性传递给 Product 组件。改变这个:

      {products.map((products) => (
                    <Grid item key={product.id} item xs={12} sm={6} md={4} lg={3}>
                      <Product/>
      

      到这里:

      {products.map((products) => (
                    <Grid item key={products.id} item xs={12} sm={6} md={4} lg={3}>
                      <Product/>
      

      为什么?因为您正在将一个对象(products)映射到一个 JSX 元素中,即 组件,所以使用 { product.id } 您正在尝试映射一个未定义的对象。 然后确保您尝试映射的 products 属性已使用 Products 组件正确传递到父组件。

      【讨论】:

        【解决方案5】:

        这是因为您将数组“products”和地图项“products”取了相同的名称。将地图项更改为“产品”之类的其他内容:

        const products = [
                {id: 1, name: 'Shoes', description: 'Running Shoes.' },
                {id: 2, name: 'MacBook', description: 'Apple MacBook.' },
            ];
        
        const Products = ({ products }) => {
          const classes = useStyles();
        
          return (
            <main className={classes.content}>
              <div className={classes.toolbar} />
              <Grid container justify="center" spacing={4}>
                {products.map((product) => (
                  <Grid item key={product.id} item xs={12} sm={6} md={4} lg={3}>
                    <Product/>
                  </Grid>
                ))};
              </Grid>
            </main>
          );
        };
        

        【讨论】:

          【解决方案6】:
                  {products && products.map((product) => (
                    <Grid item key={product.id} item xs={12} sm={6} md={4} lg={3}>
                      <Product />
                    </Grid>
                  ))};
          

          【讨论】:

          • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
          • 我有一个类似的问题,这种方法解决了。虽然与这个问题中的硬编码products 数组不同,但我从数据库中获取了一个数据数组。无论如何,这个想法是products &amp;&amp; products.map(..... 首先检查以确保products 不是未定义的(或至少具有真实值)。如果未定义,products.map 将不会执行。
          【解决方案7】:

          我遇到了同样的错误,并通过首先询问数组是否存在来解决它。

          例子:

          <Filter>
            { product.color?.map((c) => (
              <FilterColor color = {c} key = {c} />
            ))};
          </Filter>
          

          【讨论】:

            【解决方案8】:

            首先请检查是否是出口产品,如果是则

            改变

            {products.map((products) => (
              <Grid item key={product.id} item xs={12} sm={6} md={4} lg={3}>
              <Product/>

            {products.map((products) => (
               <Grid item key={products.id} item xs={12} sm={6} md={4} lg={3}>
               <Product/>

            因为在这里您将产品映射为产品,所以您必须设置像 products.id 这样的键

            【讨论】:

              【解决方案9】:

              您已经用 products 命名了 map 方法的每个迭代,但您正在使用 product 来捕获迭代的每个实例。

              【讨论】:

                【解决方案10】:

                有时,当您没有使用 useState 变量时,它会出错。 我还制作了另外两个组件。 1) 游览 2) 游览

                 const [tours, setTours ] = useState([]);
                

                现在,我使用 Tours 组件在 App.js 文件中使用了这个 tours 变量。

                App.js

                我在 App.js 文件中使用了 tours

                App.js

                现在,我将使用 tours 变量在 Tours.js 文件中使用 .map() 函数。

                const Tours = ({ tours }) => {
                /...
                {tours.map()
                .../
                

                Tours.js

                您必须检查两个文件的拼写是否相同,否则会出错,并且该特定文件的 UI 也不起作用。

                感谢阅读。

                【讨论】:

                  猜你喜欢
                  • 2021-12-31
                  • 2021-11-10
                  • 2021-11-08
                  • 2021-11-21
                  • 1970-01-01
                  • 2023-01-23
                  • 2022-01-16
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多