nest.js模板中获取async异步返回的数据

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/junbujianwpl/article/details/99701011

采用async方式获取数据库中的数据,返回的是一个Promise<T>。直接在模板中去解析Promise得到的会是一个Promise对象,而不是最终返回的结果,然后用{{message.name}}去解析是会失败的。

解决方法,所有async方法配套加上await。拿一个官网的例子。

https://docs.nestjs.com/providers

cats.controller.ts


import { Controller, Get, Post, Body } from '@nestjs/common';
import { CreateCatDto } from './dto/create-cat.dto';
import { CatsService } from './cats.service';
import { Cat } from './interfaces/cat.interface';

@Controller('cats')
export class CatsController {
  constructor(private readonly catsService: CatsService) {}

  @Post()
  async create(@Body() createCatDto: CreateCatDto) {
    this.catsService.create(createCatDto);
  }

  @Get()
  async findAll(): Promise<Cat[]> {
    return await this.catsService.findAll(); // 官网demo没有添加await关键词
  }
}

 

一个真实小项目的例子

crashes.controller.ts

import { Controller, Get, Post, Body, Param, Render, Res } from '@nestjs/common';
import { CreateCrashDto } from './dto/create-Crash.dto';
import { CrashesService } from './crashes.service';
import { Response } from 'express';
import { Crash } from './interfaces/crashes.interface';

@Controller('Crashes')
export class CrashesController {
  constructor(private readonly crashesService: CrashesService) { }


  @Get('/:version/:user')
  @Render('index')
  async findVersionUser(@Param('version') version: string, @Param('user') user: string): Promise<any> {
    let result = await this.crashesService.aggregateVersionAsync(version);
    let total = await this.crashesService.aggregateVersionCountAsync(version);

    return { message: result, total: total.length };
  }
}

index.hbs

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>App</title>
</head>

<p> group count :{{ message.length}}</p>

<body>
  <table border="1">
    <td>tip</td>
    <td>count</td>
    {{#each message}}
    <tr>
      <td><a href="/crashes/{{_id}}">{{_id}} </a> </td>
      <td> {{num}} </td>
    </tr>
    {{/each}}
  </table>
</body>

</html>

crashes.service.ts

import { Inject, Injectable } from '@nestjs/common';
import { Model } from 'mongoose';
import { CreateCrashDto } from './dto/create-crash.dto';
import { Crash } from './interfaces/crashes.interface';

@Injectable()
export class CrashesService {
  constructor(@Inject('Crash_MODEL') private readonly CrashModel: Model<Crash>) { }

  async aggregateVersionCountAsync(version: string): Promise<Crash[]> {
    return await this.CrashModel.aggregate([{ $match: { version } }]);
  }

  async aggregateVersionAsync(version: string): Promise<Crash[]> {
    return await this.CrashModel.aggregate([{ $match: { version } },
    { $group: { _id: '$tip', num: { $sum: 1 }, crashes: { $push: '$_id' } } },
    { $sort: { num: -1 } }]).exec();
  }

  async findAllVersion(): Promise<Crash[]> {
    return await this.CrashModel.aggregate([
      { $match: { _id: { $ne: '-1' }, version: { $ne: '' } } },
      { $group: { _id: '$version', num: { $sum: 1 } } },
      { $sort: { _id: -1 } }]).exec();
  }
}

 

 

展开阅读全文

没有更多推荐了,返回首页