如何在React中显示所有元素



我在项目(集合)中添加了子项目(集合)。我把其中一个元素放到了props中。但是它不能被定义。我还想循环和显示所有元素?我该怎么办?

error =>×TypeError:无法读取未定义属性' rfv6bjmg2dmibqwnzx80 '

TypeError: Cannot read property 'map' of undefined

23 |{project.answers. {地图(回答=比;{25 | return (26 | {answer.id}

https://github.com/ayush221b/MarioPlan-react-redux-firebase-app/blob/master/src/Components/projects/ProjectDetails.js

const ProjectDetails = (props) => {
const { project } = props;

if (project) {
return (
<div className="container section project-details">
<div className="card z-depth-0">
<div className="card-content">
<span className="card-title">{ project.title }</span>
<p>{ project.content }</p>
</div>
<div className="card-action gret lighten-4 grey-text">
<div>Posted by {project.userName}</div>
<div>2nd, September, 2am</div>
</div>
<-- I would to display all data of sub projects--> 
<div>{project.subproject['RFv6bJMG2dmiBqWNZX8O'].contents}</div>
{project.answers && project.answers.map(answer => {
return (
<div>{answer.id}
{answer.content}
</div>
)
</div>
</div>
)}
const mapStateToProps = (state, ownProps) => {
const id = ownProps.match.params.id;
const projects = state.firestore.data.projects;
const project = projects ? projects[id] : null;
const subprojects = project ? project.subprojects : "unloaded";

console.log("sub ="+JSON.stringify(subprojects));
return {
project: project
}
}
export default compose(
connect(mapStateToProps),
firestoreConnect(props => [
{collection: 'projects'},
{
collection: 'projects',
doc:props.match.params.id,
subcollections :[{collection:'subprojects'}]  }
]
))(ProjectDetails);

尝试:

<div>answers = {project.subproject?.['RFv6bJMG2dmiBqWNZX8O']?.contents}</div>

相关内容

  • 没有找到相关文章

最新更新