Mic*_*rst 1 mapping automapper node.js nestjs
我正在使用Chau Tran 的nestjsx-automapper ( https://automapper.netlify.app/docs/usages/init/add-profile )(感谢这段很酷的代码)。我已经按照文档中所示和这里已经讨论过的方式实现了它: How use profiles from nartc/automapper into an 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 {}Run Code Online (Sandbox Code Playgroud)
商户.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 {}Run Code Online (Sandbox Code Playgroud)
商家.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;
}
}Run Code Online (Sandbox Code Playgroud)
商家.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);
}
}Run Code Online (Sandbox Code Playgroud)
当我使用此设置运行应用程序时,出现以下异常: Nest 无法解析 AutomapperModule (?) 的依赖关系。请确保索引 [0] 处的参数 AutomapperExplorer 在 AutomapperModule 上下文中可用。
AutoMapperModule.withMapper()inAppModule是您唯一需要使用的时间AutoMapperModule。
withMapper()当您想在 a (或任何)中使用时AutoMapper,可以通过创建一个单例。@InjectMapper()MapperServiceInjectable
至于Profile,以下是正确的语法:
@Profile()
export class MerchantProfile extends ProfileBase {
constructor(mapper: AutoMapper) { // no private readonly.
super();
mapper.createMap(Merchant, MerchantDto);
}
// no configure() method
}
Run Code Online (Sandbox Code Playgroud)
下面是写@nartc/automapper在那里的源代码addProfile():
addProfile(profile: new (mapper: AutoMapper) => MappingProfile): AutoMapper {
this._profileStorage.add(this, new profile(this));
return this;
}
Run Code Online (Sandbox Code Playgroud)
您可以在内部看到,@nartc/automapper将实例化 ( new profile())并将实例传递AutoMapper给 Profile 的构造函数,以便您可以在Profile's constructor
对于你的这段代码 MerchantModule
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';
// this is all you need which is to import the profile so TypeScript can execute it. Don't need `MerchantProfile` at all
import './profiles/merchant.profile';
import { AutoMapper, AutomapperModule } from 'nestjsx-automapper';
@Module({
imports: [TypeOrmModule.forFeature([Merchant])], // don't need to import AutoMapperModule again. MerchantProfile is not a Module so you can't import it
exports: [TypeOrmModule],
controllers: [MerchantController],
providers: [MerchantService]
})
export class MerchantModule {}
Run Code Online (Sandbox Code Playgroud)
在您的MerchantController:
@Controller('merchant')
export class MerchantController {
constructor(
private merchantService: MerchantService,
@InjectMapper() private readonly mapper: AutoMapper) { }
@Get()
public async findAll(): Promise<MerchantDto[]> {
// make sure `this.merchantService.find()` returns an Array of
// Merchant instances. If not, please provide an extra param to map()
// return this.mapper.mapArray(await this.merchantService.find(),MerchantDto);
return this.mapper.mapArray(await this.merchantService.find(), MerchantDto, Merchant); // notice the 3rd parameter is the Source model.
}
}
Run Code Online (Sandbox Code Playgroud)
请让我知道这是否适合您。如果没有,请在nestjsx-automapperrepo 中创建一个问题并提供一个可重现的存储库,我会尽快查看。
| 归档时间: |
|
| 查看次数: |
1550 次 |
| 最近记录: |