[英]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.