【发布时间】:2020-05-02 04:03:38
【问题描述】:
我的地图函数中给出了错误。
我正在关注reactjs 教程,当将值从一个组件的状态传递到另一个组件时,我一直遇到问题。
我正在使用axios.get() 连接到后端。
export default class Viewcustomer extends React.Component{
constructor(props) {
super(props)
this.state = {
Id:"",
name:"",
fax:"",
NIC: "",
type: "",
email: "",
website: "",
address: "",
phoneNo: "",
DOB: "",
note: "",
customer:[]
}
}
onChange = (e) => {
this.setState({[e.target.name]: e.target.value})
}
componentDidMount() {
axios.get(`http://localhost:5001/customer/view`)
.then(res => {
const customers = res.data;
this.setState({ customers });
})
}
render(){
return(
<table width="100%">
<tr>
<th>Id</th>
<th>name</th>
<th>fax</th>
<th>NIC</th>
<th>type</th>
<th>email</th>
<th>address</th>
<th>phoneNo</th>
<th>DOB</th>
<th>note</th>
</tr>
//Error raised by this line of code
{ this.state.customers.map(customer =>
<tr>
<td>{customer.Id}</td>
<td>{customer.name}</td>
<td>{customer.fax}</td>
<td>{customer.NIC}</td>
<td>{customer.type}</td>
<td>{customer.email}</td>
<td>{customer.address}</td>
<td>{customer.phoneNo}</td>
<td>{customer.DOB}</td>
<td>{customer.note}</td>
</tr>)}
</table>
)
}
}
编辑
错误与标有引号的行有关,由 map() 函数引发,但根本问题是 this.state.customers 是 undefined。
【问题讨论】:
-
在您的
this.state声明中您打错字了,customer应该是customers。 -
这能回答你的问题吗? Cannot read property 'map' of undefined
标签: javascript reactjs