我正在尝试使用句柄从 db typescript Nestjs 显示项目(事件),事件是对象数组。每个对象的属性未定义

问题描述 投票:0回答:1

请查看我通过事件检查问题的方式

@Get('视图/用户/事件')

异步查找所有事件(

@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(页:数量 = 1,限制:数量 = 10,名称?:字符串,类型?:字符串) {

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.');

  }

  

}

}

这是车把页面

{{#每个事件}}

{{this.event_name}}

{{这个.描述}}

{{这个位置}}

{{this.event_date}}

{{this._id}}

{{/每个}}

{{#每个事件}}

{{this.event_name}}

{{这个.描述}}

{{这个位置}}

{{this.event_date}}

{{this._id}}

这是对象数组

[

{

_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

}

]

arrays object properties handlebars.js undefined
1个回答
0
投票

是的,我找到了答案。

只需将 .lean() 添加到查找中

© www.soinside.com 2019 - 2024. All rights reserved.