I am trying to return a page that shows a users routines. I want a message to display to the user that they do not have any made routines. The problem is in my else statement, I can only return console.error. If I do some HTML, it will return that for the entire routines array of what I sent over. I just want it to return a single "No activities found, create one above."
routines.map((routine) => {
if (currentUser === routine.creatorName) {
return (
<div className="routineCard">
<h3>Routines</h3>
<p>Name: {routine.name}</p>
<p>goal: {routine.goal}</p>
<p>User: {routine.creatorName}</p>
<ul > Activities
<li className="routineActivityCards">{
routine.activities.map((activity) => {
return (
<div className="routineActivityCard">
<ul>
<h5>{activity.name}</h5>
<p>Info: {activity.description}</p>
<p>Count: {activity.count}</p>
<p>Duration: {activity.duration}</p>
</ul>
</div>
)
}
)
}</li>
</ul>
</div>
)
} else {
return console.error;
}
})
return (
<div>
<h1>No activites found</h1>
</div>
)
I put this in the else, and it returns
No activites found
No activites found
No activites found
No activites found etc...
Maybe if I set my return to just return routine[0]? I just do not know how to exactly word that into my code. Thanks for the help everyone!
2
Answers
First,
filter
the array to get all the matching entries. If the length of the result is 0, then show the message.Just add a try-catch statement, so you can break the loop generated by the
Array.prototype.map()
method (which does not support thebreak
keyword itself).An alternative (and maybe it’s the simplest way) is to use a "normal" loop, which supports the
break
keyword, this way:by LL