nest.js模板中獲取async異步返回的數據

採用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();
  }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章