Home > Net >  React: How to loop through Array of Objects in MongoDb
React: How to loop through Array of Objects in MongoDb

Time:06-30

i want to go through an Array of Objects in MongoDB (for Schema look at the picture.)

I want to check if the User is participant of an Event and if, the event should be displayed on a page .

My idea was

{events.filter(events => ( events.participants.email === currentProfile.email)).
               map((events)=>(
                 <Grid key={events._id} item xs={12} sm={6} md={4}>
                 <Event event={events} setCurrentId2={setCurrentId2} />

but does not work. I dont have any idea how to solve it.

CodePudding user response:

events.participants is an array and you are doing events.participants.email wich is undefined, to do what you want:

{events
        .filter((e) => e.participants.some((p) => p.email === currentProfile.email))
        .map((event) => (
          <>
            <Grid key={event._id} item xs={12} sm={6} md={4}>
            <Event event={event} setCurrentId2={setCurrentId2} />
          <>
        ))}

With this, each event is iterating the partcipants field looking for the current user's email

  • Related