简体   繁体   English

未捕获的类型错误:无法读取未定义的属性(读取“地图”)

[英]Uncaught TypeError: Cannot read properties of undefined (reading 'map')

I am getting this error我收到此错误

Mainsection.js:27 Uncaught TypeError: Cannot read properties of undefined (reading 'map') Mainsection.js:27 未捕获类型错误:无法读取未定义的属性(读取“地图”)

Here is my mainsection.js file, I am using API key to iterate the data, I am still not getting the cause of the error.这是我的 mainsection.js 文件,我正在使用 API 键来迭代数据,我仍然没有得到错误的原因。 First I had made an array whose name was info and stored all the data in it and then iterated it, Now after using fetchapi, I deleted that array as it was of no use.首先,我创建了一个名称为 info 的数组并将所有数据存储在其中,然后对其进行迭代,现在使用 fetchapi 后,我删除了该数组,因为它没有用。 I don't know whether it was meant to delete or not.不知道是不是要删。

import React, { Component } from 'react'
import Card from './Card'

export default class Mainsection extends Component {

    constructor() {
        super();
        this.state = {
            info:null
        }
    }
async componentDidMount(){
    let Url="https://randomuser.me/api/?inc=gender,name,nat,location,picture,email&results=";
    let data= await fetch(Url);
    let parsedData= await data.json()
    console.log(parsedData);
    this.setState({
         info : parsedData.info
    })

}

    render() {
        return (
            <div>
                <div className="container mt-5">
                    <div className="row">
                        {this.state.info.map((element) => {
                            return <div className="col-md-4">
                                <Card key={element.email} name={element.name} location={element.location} gender={element.gender} imageUrl={element.picture.medium} />
                            </div>
                        })}

                    </div>
                </div>
            </div>
        )
    }
}

And here is my card.js file这是我的 card.js 文件

import React, { Component } from 'react'

export default class Card extends Component {
    render() {
        let {name, location, gender, imageUrl}=this.props
        return (
            <div>
                 <div className="card" style={{ width: "18rem" }}>
            <img src={imageUrl} className="card-img-top" alt="..." />
            <div className="card-body">
                <h5 className="card-title">{name}</h5>
                <p className="card-text">{location}</p>
                <p className="card-text">{gender}</p>
                <a href="/" className="btn btn-primary">Go somewhere</a>
            </div>
        </div>
            </div>
        )
    }
}

Please let me I should provide more details请让我提供更多详细信息

You can find a working example here: https://codesandbox.io/s/musing-hill-uxtt0您可以在这里找到一个工作示例: https://codesandbox.io/s/musing-hill-uxtt0

There are other issues with your code.您的代码还有其他问题。 For example, the name and location are objects and you are directly trying to show it on UI.例如,名称和位置是对象,您直接尝试在 UI 上显示它。 I have also added code to fix name.我还添加了代码来修复名称。

Mainsection.js Mainsection.js

import React, { Component } from "react";
import Card from "./Card";

export default class Mainsection extends Component {
  constructor() {
    super();
    this.state = {
      info: null,
      results: null
    };
  }
  async componentDidMount() {
    let Url =
      "https://randomuser.me/api/?inc=gender,name,nat,location,picture,email&results=";
    let data = await fetch(Url);
    let parsedData = await data.json();
    console.log(parsedData);
    this.setState({
      info: parsedData.info,
      results: parsedData.results
    });
  }

  render() {
    console.log("results : ", this.state.results);
    return (
      <div>
        <div className="container mt-5">
          <div className="row">
            {this.state?.results?.map((element) => {
              return (
                <div className="col-md-4">
                  <Card
                    key={element.email}
                    name={element.name}
                    location={element.location}
                    gender={element.gender}
                    imageUrl={element.picture.medium}
                  />
                </div>
              );
            })}
          </div>
        </div>
      </div>
    );
  }
}

Card.js Card.js

import React, { Component } from "react";

export default class Card extends Component {
  render() {
    let { name, location, gender, imageUrl } = this.props;
    return (
      <div>
        <div className="card" style={{ width: "18rem" }}>
          <img src={imageUrl} className="card-img-top" alt="..." />
          <div className="card-body">
            <h5 className="card-title">{`${name?.title} ${name?.first} ${name?.last}`}</h5>
            <p className="card-text">{JSON.stringify(location)}</p>
            <p className="card-text">{gender}</p>
            <a href="/" className="btn btn-primary">
              Go somewhere
            </a>
          </div>
        </div>
      </div>
    );
  }
}

So, the API you are using doesn't response back with an Array .因此,您使用的API不会以Array响应。 But it responses with an object , that object has 2 attributes which are results and info .但它以object响应,即object有 2 个attributes ,即resultsinfo as you can see below正如你在下面看到的

{
"results": ...
....
....
"info": ...
}

and results it self is an Array of objects , while info is just an object. results它本身是一个Array of objects ,而info只是一个 object。

So yes, you cant use map on object, its only useable on arrays.所以是的,你不能在 object 上使用 map,它只能在 arrays 上使用。 Check the response carefully so you can decide what you wanna do.仔细检查回复,以便您决定要做什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法读取未定义的属性(读取“地图”)反应 - Uncaught TypeError: Cannot read properties of undefined (reading 'map') React 错误:未捕获的类型错误:无法读取未定义的属性(读取“地图”) - Error: Uncaught TypeError: Cannot read properties of undefined (reading 'map') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '') 未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”) - Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter') Uncaught TypeError TypeError:无法读取未定义的属性(读取“路径”) - Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'path') 未捕获的类型错误:无法读取未定义的属性(读取“目标”)和(读取“值”) - Uncaught TypeError: Cannot read properties of undefined (reading 'target') & (reading 'value') 未捕获的类型错误:无法读取未定义的属性(读取“未定义”) - Uncaught TypeError: Cannot read properties of undefined (reading 'undefined')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM