Trying to log array elements, I'm getting the following error:
TypeError: Cannot read property '0' of undefined
However, when I CAN log the project.tasks array object in console. But it can't log the elements.
The array seems to exist as I can log the object but NOT the elements. I'm confused. Ideas?
CODE SANDBOX: https://codesandbox.io/s/blissful-mclaren-ll5wo
function EditProjectView(props) {
const [project, setProject] = useState({});
// get id from params
const { id } = useParams();
// syncronously (same time before and then after) renders it will use this hook
useLayoutEffect(() => {
// fake* ajax request to database
const data = database.projects.find((project) => {
return project.id == 123;
}, []);
setProject(data);
});
if (project) {
return (
<React.Fragment>
<main style={classes.content}>
<div style={classes.projectContainer}>
<div style={classes.projectOverviewWrapper}>
<Paper elevation={2}>
<div style={classes.project}>
{/* {<ProjectDetailsView project={project} />} */}
</div>
</Paper>
</div>
<div style={classes.tasksContainer}>
<h3>ALL TASKS</h3>
{console.log(
"prints out array object successfully i.e. [{..}, {..]] <-------------",
project.tasks
)}
{console.log(
"does NOT print array object's elements FAIL <-----------",
project.tasks[0].taskName
)}
<TaskExpansionPanel
panelTitle={
<PanelTitle
// totalTimeInMinutes={task.totalTimeInMinutes}
// taskNumber={task.taskNumber}
// taskName={task.taskName}
// taskName={project.projectName}
/>
}
panelDetails={<PanelDescription />}
/>
<CreateTaskBtn />
</div>
</div>
</main>
</React.Fragment>
);
} else {
return <p>loading..</p>;
}
}