【发布时间】:2020-08-11 03:23:06
【问题描述】:
我正在使用 Chau Tran 的 nestjsx-automapper (https://automapper.netlify.app/docs/usages/init/add-profile)(感谢那段很酷的代码)。我已经按照文档中所示的方式实现了它,并且已经在此处讨论过: How use profiles from nartc/automapper into a nestjs application
但我仍然有一个从我的 Profile 类中访问 AutoMapper 的问题。
这是我的设置:
app.module.ts:
import { Module } from '@nestjs/common';
import { AppService } from './app.service';
import { MerchantModule } from './merchant/merchant.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AutomapperModule, AutoMapper } from 'nestjsx-automapper';
@Module({
imports: [
TypeOrmModule.forRoot({
...
}),
AutomapperModule.withMapper(),
MerchantModule
],
providers: [],
controllers: [],
})
export class AppModule {}
merchant.module.ts:
import { Module } from '@nestjs/common';
import { MerchantController } from './merchant.controller';
import { MerchantService } from './merchant.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Merchant } from './entities/merchant.entity';
import { MerchantProfile } from './profiles/merchant.profile';
import { AutoMapper, AutomapperModule } from 'nestjsx-automapper';
@Module({
imports: [TypeOrmModule.forFeature([Merchant]), AutomapperModule, MerchantProfile],
exports: [TypeOrmModule],
controllers: [MerchantController],
providers: [MerchantService]
})
export class MerchantModule {}
merchant.profile.ts:
import {
ProfileBase,
Profile,
AutoMapper
} from 'nestjsx-automapper';
import { Merchant } from '../entities/merchant.entity';
import { MerchantDto } from '../dto/merchant.dto';
@Profile()
export class MerchantProfile extends ProfileBase {
constructor(
private readonly mapper: AutoMapper) {
super();
mapper.createMap(Merchant, MerchantDto);
}
configure(): void {
return null;
}
}
merchant.controller.ts:
import { Controller, Get, Param, Post, Body, Put, Delete } from '@nestjs/common';
import { MerchantService } from './merchant.service';
import { Merchant } from './entities/merchant.entity';
import { MerchantDto } from './dto/merchant.dto';
import { DeleteResult } from 'typeorm';
import { AutoMapper, InjectMapper } from 'nestjsx-automapper';
@Controller('merchant')
export class MerchantController {
constructor(
private merchantService: MerchantService,
@InjectMapper() private readonly mapper: AutoMapper) { }
@Get()
public async findAll(): Promise<MerchantDto[]> {
return this.mapper.mapArray(await this.merchantService.find(),MerchantDto);
}
}
当我使用此设置运行应用程序时,出现以下异常: Nest 无法解析 AutomapperModule (?) 的依赖关系。请确保索引 [0] 处的参数 AutomapperExplorer 在 AutomapperModule 上下文中可用。
【问题讨论】:
标签: node.js mapping automapper nestjs