-1

please look at the way i am passing the events check for the issue

@Get('views/users/events')

async findAllEvents(

@Req() req: Request,

@Res() res: Response,

@Query('page') page: number = 1,

@Query('limit') limit: number = 10,

@Query('name') name?: string,

@Query('type') type?: string,

) {

try {

  if (req.cookies.user_jwt) {

    return res.redirect('users-events');

  }

  const response = await this.eventsService.findAll(page, limit, name, type);

  // console.log(response.events);

  return res.render('users-events', {events: response.events, current: page, pages: response.pages, messages: req.flash() });

} catch (error) {

  if (error instanceof NotFoundException) {

    req.flash('error', error.message);

    return res.render('users-events');

  } else {

    console.error('Error retrieving events:', error);

    req.flash('error', error.message);

    return res.render('users-events');

  }

}

}

async findAll(page: number = 1, limit: number = 10, name?: string, type?: string) {

try {

  const currentDate = new Date();

  const skip = (page - 1) * limit;



  const query: any = { event_date: { $gte: currentDate } };



  if (name || type) {

    query.$or = [];

    if (name) {

      query.$or.push({ event_name: { $regex: new RegExp(name, 'i') } });

    }

    if (type) {

      query.$or.push({ event_type: { $regex: new RegExp(type, 'i') } });

    }

  }

  

  const events = await this.eventModel

    .find(query)

    .select('_id image_url event_name event_type description location sponsors event_date')

    .skip(skip)

    .limit(limit)

    .exec();



  if (!events || events.length === 0) {

    throw new NotFoundException('There are no upcoming events at this time.');

  }



  // Calculate total pages

  const totalPages = Math.ceil(events.length / limit);



  return {

    message: 'Events retrieved successfully.',

    totalPages: totalPages,

    pages: `${page} of ${totalPages}`,

    events: events,

    statusCode: HttpStatus.OK

  }

} catch (error) {

  if (error.name === 'NotFoundException') {

    throw new NotFoundException(error);

  } else {

    console.error('Error retrieving events:', error);

    throw new Error('Error retrieving events.');

  }

  

}

}

Here is the handlebar page

{{#each events}}

{{this.event_name}}

{{this.description}}

{{this.location}}

{{this.event_date}}

{{this._id}}

{{/each}}

{{#each events}}

{{this.event_name}}

{{this.description}}

{{this.location}}

{{this.event_date}}

{{this._id}}

This is the array of object

[

{

_id: new ObjectId('65fb4479787dbd450bb0f640'),

image_url: 'https://res.cloudinary.com/ddl8i/image/upload/v1710922731/aubjuene3kjkiaggenmu.png',

event_name: 'Dev Fest',

event_type: 'Tech',

description: 'Coding is magic',

location: 'Lagos',

sponsors: [],

event_date: 2024-04-04T00:00:00.000Z

}

]

1 Answer 1

0

Yes I found the answer.

Just add .lean() to the find

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.