如何解决“无法读取未定义的属性‘地图’”
TypeError: 无法读取未定义的属性 'map' 教育
import { deleteEducation } from '../../actions/profile';
const Education = ({ education,deleteEducation }) => {
const educations = education.map(edu => (
<tr key={edu._id}>
<td>{edu.school}</td>
<td className="hide-sm">{edu.degree}</td>
<td>
这是我的education.js代码
import React,{ Fragment } from 'react';
import PropTypes from 'prop-types';
import Moment from 'react-moment';
import moment from 'moment';
import { connect } from 'react-redux';
import { deleteEducation } from '../../actions/profile';
const Education = ({ education,deleteEducation }) => {
const educations = education.map(edu => (
<tr key={edu._id}>
<td>{edu.school}</td>
<td className="hide-sm">{edu.degree}</td>
<td>
<Moment format="YYYY/MM/DD">{moment.utc(edu.from)}</Moment> -{' '}
{edu.to === null ? (
' Now'
) : (
<Moment format="YYYY/MM/DD">{moment.utc(edu.to)}</Moment>
)}
</td>
<td>
<button
onClick={() => deleteEducation(edu._id)}
className="btn btn-danger"
>
Delete
</button>
</td>
</tr>
));
return (
<Fragment>
<h2 className="my-2">Education Credentials</h2>
<table className="table">
<thead>
<tr>
<th>School</th>
<th className="hide-sm">Degree</th>
<th className="hide-sm">Years</th>
<th />
</tr>
</thead>
<tbody>{educations}</tbody>
</table>
</Fragment>
);
};
Education.propTypes = {
education: PropTypes.array.isrequired,deleteEducation: PropTypes.func.isrequired
};
export default connect(
null,{ deleteEducation }
)(Education);
解决方法
试试这个
const educations = education && education.map(edu => .....
如果教育字段不存在并且显示空白屏幕而不是错误,则使用此选项。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。