简体   繁体   English

我在我的 React 项目中收到此错误“TypeError: Cannot read property 'map' of undefined”

[英]I'm getting this error “TypeError: Cannot read property 'map' of undefined” on my React project

I'm new on React.我是 React 的新手。 I have a node.js API.我有一个 node.js API。 I'm trying to create a login page.我正在尝试创建一个登录页面。 I make a request to the API and I can get a response.我向 API 提出请求,我可以得到回复。 I put the returned answer into the state named "user".我将返回的答案放入名为“user”的 state 中。 I want to print this to an tag with the map function and check the data.我想用 map function 将其打印到标签上并检查数据。 However, I am getting the error "TypeError: Cannot read property 'map' of undefined."但是,我收到错误“TypeError:无法读取未定义的属性 'map'。” I have defined the state with [] and I don't know what else to do.我已经用 [] 定义了 state,但我不知道还能做什么。

enter image description here在此处输入图像描述

import React, { Component } from 'react'

export default class App extends Component {

  state = { user: [] }

  componentDidMount(){
    this.getLogin();
  }
   
  getLogin = () =>{
    let data = { 
      "email": "xxxxxx@gmail.com",
      "password": "123456"
    }

    fetch("http://localhost:5000/api/auth/login",{
      method:'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    })
    .then(response => response.json())
    .then(response => console.log(response))
    .then(data => this.setState({ user : data }));    
  }
   
  render() {
      return (
          <div>
              {this.state.user.map(data => (
                <h3> {data.data.access_token} </h3>
              ))}
          </div>
      )
  }
}

{ {

I add the answer returned from the API here.我在这里添加从 API 返回的答案。

  success: true, access_token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVmY…I5M30.9r9iRA0lyXTy-MjUr2QN9RrxGneqyUoVZM8eFeXoK1s", data: {…}}
    access_token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVmYzNmYmFiNjFhOWQzMGQwNDNjY2I0OSIsIm5hbWUiOiJOZW1yYW4gQWtzYWthbCIsImlhdCI6MTYwOTc5MDI5M30.9r9iRA0lyXTy-MjUr2QN9RrxGneqyUoVZM8eFeXoK1s"
    data: {name: "Red Reddington", email: "example@gmail.com"}
    success: true
    __proto__: Object

Got to function getLogIn and remove this line.then(response => console.log(response))到 function getLogIn 并删除此行。then(response => console.log(response))

delete the code provided in your file删除文件中提供的代码

  .then(response => console.log(response))

You are not making sure you are iterating over an array.你不确定你正在迭代一个数组。 All is happening because chaining then in the promise.一切都在发生,因为在 promise 中链接。 what a then returns is converted into the input for the next then . a then返回的内容被转换为下一个then的输入。 So the console.log one is not returning anything hence the following next is getting undefined .因此, console.log没有返回任何内容,因此接下来的内容是undefined Once you solved that issue by removing the log or returning again the response, you need to make sure to save the parts from you need from response into the state.通过删除日志或再次返回响应解决了该问题后,您需要确保将响应中所需的部分保存到 state 中。

state = { user: [], access_token: '' }

fetch("http://localhost:5000/api/auth/login",{
      method:'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
})
.then((response) => response.json())
.then((response) => { console.log(response); return response; })
.then((response) => this.setState({ 
    user : response.data,
    access_token: response.access_token 
})); 

Note: make sure you store access_token to make it reactive into your template.注意:确保存储 access_token 以使其反应到您的模板中。

暂无
暂无

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

相关问题 在执行 react-redux 项目时出现此错误。 错误:- TypeError:无法读取 React-Redux 项目中未定义的属性“地图” - Getting this error while doing react-redux project. Error:- TypeError: Cannot read property 'map' of undefined in React-Redux project 尝试 map 我的道具时,我收到无法读取未定义错误的“练习”属性 - I'm getting a Cannot read property of 'exercises' of undefined error when trying to map my props 获取 TypeError: Cannot read property 'localeCompare' of undefined for my sortBy function 在我的 React 组件中,我不知道为什么 - Getting TypeError: Cannot read property 'localeCompare' of undefined for my sortBy function in my React component and I'm not sure why 错误:未处理的拒绝(类型错误):无法读取未定义的属性“地图”不确定我错过了什么 - Error: Unhandled Rejection (TypeError): Cannot read property 'map' of undefined not sure what I'm missing 我在 React 中收到“TypeError: Cannot read properties of undefined(reading: 0)” - I'm getting "TypeError: Cannot read properties of undefined(reading: 0)" in React 获取TypeError:无法读取未定义错误的属性“map” - Getting TypeError: Cannot read property 'map' of undefined error TypeError:无法读取未定义的属性“公会”我正在编写欢迎代码,但我不断收到该错误 - TypeError: Cannot read property 'guild' of undefined I'm writing a welcome code but I keep getting that error React.js 错误:TypeError:无法读取未定义的属性“地图” - React.js error :TypeError: Cannot read property 'map' of undefined 我试图 map 数据库中的数据并收到此错误。 TypeError:无法读取未定义的属性“地图” - I trying to map the data from the database and getting this error. TypeError: Cannot read property 'map' of undefined 错误:类型错误:无法读取 React-Redux 中未定义的属性“map” - Error: TypeError: Cannot read property 'map' of undefined in React-Redux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM