我有一个API的数据,我想映射它的值。但无法做到。我收到“无法读取未定义的属性'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的值不会显示
但是在作物详细信息中有三个条目,因此创建了三个表行。我什至检查了道具,所有值都存在其中。
答案 0 :(得分: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
是否存在,然后仅将其映射以跳过它(为空),或者按照注释中其他用户的建议,尝试登录render查看this.props.values.Crop_Details
是否实际存在。
答案 1 :(得分:0)
我认为您的地图函数使用了错误的数据,并且会导致此类错误(我想您应该删除Crop_Details并在项目的地图函数中使用它)。
地图功能需要一个“ 列表”。
通过地图逐步编写代码,并使用console.log(item)检查每一步。
例如:
this.props.value.map(item=>console.log(item))
this.props.value.Crop_Details.map(item=>console.log(item))
已更新
您不能将print(通过console.log)用于Array和Object。它像[object object]一样返回sth。 因此,您可以使用点表示法,散布对象或对数据使用另一种套印来打印它。
请注意在地图中使用索引
不要忘记在地图上返回的物品中使用键道具。 有关地图功能和键的更多信息,请检查此链接: https://reactjs.org/docs/lists-and-keys.html
答案 2 :(得分:0)
首先,您需要验证数据,因为该变量未定义,所以您将收到该错误。将您的代码更改为以下内容。您在问题季节的输出中使用的<td className="farmer-table-data">{item.season}</td>
以大写字母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>
);
}
}