无法解决错误无法读取 react js 中未定义的属性“map”
2019-04-21
137
我有来自 API 的数据,我想映射它的值。但无法这样做。我收到“无法读取未定义的属性‘map’”错误。我无法弄清楚为什么会收到此错误
我尝试了所有可能的解决方案。如果代码中没有 map 函数,则代码可以正常工作。
class MyVerticallyCenteredModal extends React.Component {
render() {
return (
<Modal
{...this.props}
size="lg"
aria-labelledby="contained-modal-title-vcenter"
dialogClassName="farmer-modal"
centered
>
<Modal.Header closeButton>
<Modal.Title id="contained-modal-title-vcenter">
DETAILS
</Modal.Title>
</Modal.Header>
<Modal.Body>
<h5>Farm No: <span className="header-values"> {this.props.values.Farm_No}</span> </h5>
<h5>Farmer No: <span className="header-values"> {this.props.values.Farmer_Reg_Number}</span> </h5>
<h5>Farmer Name: <span className="header-values"> {this.props.values.Farmer_Name} </span> </h5>
<h5>Village: <span className="header-values"> {this.props.values.Farmer_Village} </span> </h5>
<h5>Farm Area: <span className="header-values"> {this.props.values.Farm_Area} </span> </h5>
<br/>
<table className="farmer-table">
<tr className="farmer-table-row">
<th className="farmer-table-header">Season</th>
<th className="farmer-table-header">Crop Name</th>
<th className="farmer-table-header">Crop Area</th>
<th className="farmer-table-header">Crop Type</th>
<th className="farmer-table-header">Estimated Quantity</th>
<th className="farmer-table-header">Organic Status</th>
<th className="farmer-table-header">Crop Total</th>
</tr>
{
this.props.values.Crop_Details.map((item, key) => (
<div>
<tr className="farmer-table-row">
<td className="farmer-table-data">{item.season}</td>
</tr>
</div>
))}
</table>
</Modal.Body>
<Modal.Footer>
<Button onClick={this.props.onHide}>Close</Button>
</Modal.Footer>
</Modal>
);
}
}
这是来自 API 的响应
Crop_Details: Array(3)
0: {Season: "Perennial", Organic_Status: "Organic", Crop_Name: "BananaFresh", Crop_Type: "Inter", Crop_Area: "0.000000", …}
1: {Season: "Perennial", Organic_Status: "Organic", Crop_Name: "Blackpepper(ungarbled)", Crop_Type: "Inter", Crop_Area: "0.000000", …}
2: {Season: "Perennial", Organic_Status: "Organic", Crop_Name: "CoffeeArabica-Cherry", Crop_Type: "Main", Crop_Area: "0.200000", …}
length: 3
Farm_Area: "0.200078750"
Farm_No: "KA28tdfg401"
Farmer_Name: "Lakshmamma"
Farmer_Reg_Number: "KA2301075ub74"
Farmer_Village: "Dbhyt"
现在我解决了错误。但仅 Crop_Details 的值并未显示
但作物详情中有三个条目,因此创建了三个表行。我甚至检查了道具并且所有价值都存在于其中。
2个回答
从您的代码中我可以看出,当您的 DOM 首次呈现时,
this.props.values
不存在。
您不妨尝试这样的方法
class MyVerticallyCenteredModal extends React.Component {
render() {
return (
<Modal
{...this.props}
size="lg"
aria-labelledby="contained-modal-title-vcenter"
dialogClassName="farmer-modal"
centered
>
<Modal.Header closeButton>
<Modal.Title id="contained-modal-title-vcenter">
DETAILS
</Modal.Title>
</Modal.Header>
<Modal.Body>
<h5>Farm No: <span className="header-values"> {this.props.values.Farm_No}</span> </h5>
<h5>Farmer No: <span className="header-values"> {this.props.values.Farmer_Reg_Number}</span> </h5>
<h5>Farmer Name: <span className="header-values"> {this.props.values.Farmer_Name} </span> </h5>
<h5>Village: <span className="header-values"> {this.props.values.Farmer_Village} </span> </h5>
<h5>Farm Area: <span className="header-values"> {this.props.values.Farm_Area} </span> </h5>
<br/>
<table className="farmer-table">
<tr className="farmer-table-row">
<th className="farmer-table-header">Season</th>
<th className="farmer-table-header">Crop Name</th>
<th className="farmer-table-header">Crop Area</th>
<th className="farmer-table-header">Crop Type</th>
<th className="farmer-table-header">Estimated Quantity</th>
<th className="farmer-table-header">Organic Status</th>
<th className="farmer-table-header">Crop Total</th>
</tr>
{
this.props.values.Crop_Details? (
this.props.values.Crop_Details.map((item, key) => (
<div>
<tr className="farmer-table-row">
<td className="farmer-table-data">{item.season}</td>
</tr>
</div>
))}): null }
</table>
</Modal.Body>
<Modal.Footer>
<Button onClick={this.props.onHide}>Close</Button>
</Modal.Footer>
</Modal>
);
}
}
在上面的代码中,我使用了三元运算符来查看
this.props.values.Crop_Details
是否存在,然后仅映射它,否则跳过它(null)或按照评论中其他用户的建议,尝试登录渲染以查看
this.props.values.Crop_Details
是否实际存在。
Alwaysblue
2019-04-21
首先,您需要验证数据,出现该错误是因为变量未定义。将您的代码更改为以下内容。您正在使用问题输出中的
<td className="farmer-table-data">{item.season}</td>
,其中 season 以大写字母
S
class MyVerticallyCenteredModal extends React.Component {
state = {
cropDetails: [],
Farm_No: '',
Farmer_Reg_Number: '',
Farmer_Name: '',
Farmer_Village: '',
Farm_Area: '',
show: false
}
componentDidMount = () => {
// fetch data or get data from props
const data = this.props.values;
// validate your data
this.setState({
cropDetails: data && data.Crop_Details || [],
Farm_No: data && data.Farm_No || '',
Farmer_Reg_Number: data && data.Farmer_Reg_Number || '',
Farmer_Name: data && data.Farmer_Name || '',
Farmer_Village: data && data.Farmer_Village || '',
Farm_Area: data && data.Farm_Area || '',
})
}
render() {
const items = this.state.cropDetails.map((item, key) => (
<tr>
<td>{item.Season}</td>
<td>{item.Crop_Name}</td>
<td>{item.Crop_Area}</td>
<td>{item.Crop_Type}</td>
</tr>
));
return (
<Modal
{...this.props}
size="lg"
aria-labelledby="contained-modal-title-vcenter"
dialogClassName="farmer-modal"
centered
>
<Modal.Header closeButton>
<Modal.Title id="contained-modal-title-vcenter">
DETAILS
</Modal.Title>
</Modal.Header>
<Modal.Body>
<h5>Farm No: <span className="header-values"> {this.state.Farm_No}</span> </h5>
<h5>Farmer No: <span className="header-values"> {this.state.Farmer_Reg_Number}</span> </h5>
<h5>Farmer Name: <span className="header-values"> {this.state.Farmer_Name} </span> </h5>
<h5>Village: <span className="header-values"> {this.state.Farmer_Village} </span> </h5>
<h5>Farm Area: <span className="header-values"> {this.state.Farm_Area} </span> </h5>
<br />
<table className="farmer-table">
<tr className="farmer-table-row">
<th className="farmer-table-header">Season</th>
<th className="farmer-table-header">Crop Name</th>
<th className="farmer-table-header">Crop Area</th>
<th className="farmer-table-header">Crop Type</th>
<th className="farmer-table-header">Estimated Quantity</th>
<th className="farmer-table-header">Organic Status</th>
<th className="farmer-table-header">Crop Total</th>
</tr>
{items}
</table>
</Modal.Body>
<Modal.Footer>
<Button onClick={this.props.onHide}>Close</Button>
</Modal.Footer>
</Modal>
);
}
}
Junius L
2019-04-21