在nest.js中使用nestjsx-automapper的Profiles。

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

我正在使用 nestjsx-automapper (https:/automapper.netlify.appdocsusagesinitadd-profile。)(感谢那段很酷的代码)。我已经按照文档中的描述和这里的讨论实现了它。如何使用nartcautomapper的配置文件到一个nestjs应用程序中。

但我还是有一个 从我的资料中访问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
2个回答
2
投票

AutoMapperModule.withMapper()AppModule 是你唯一需要使用的时候。AutoMapperModule.

withMapper() 创建一个 AutoMapper 将通过 @InjectMapper() 当您想使用 Mapper 在...中 Service (或任何 Injectable).

至于 Profile,以下是正确的语法。

@Profile()
export class MerchantProfile extends ProfileBase {
  constructor(mapper: AutoMapper) { // no private readonly.
    super();
    mapper.createMap(Merchant, MerchantDto);
  }
  // no configure() method
}

以下是正确的语法: @nartc/automapper 源码 addProfile() 是这样写的。

addProfile(profile: new (mapper: AutoMapper) => MappingProfile): AutoMapper {
    this._profileStorage.add(this, new profile(this));
    return this;
}

你可以看到内部。@nartc/automapper 将实例化(new profile())并传入 AutoMapper 实例到Profile的构造函数中,这样您就可以在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 {}

在你的 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.
    }
}

请告诉我这对你是否有效。如果不行,请在 nestjsx-automapper repo,并提供一个可复制的仓库,我会尽快看一下。


0
投票

我不太熟悉 AutoMappModule 工作,但它看起来像在你的 MerchantModule 您导入 AutoMapperModule 而你 传入 AutoMapperModule.withMapper(),类似于你在 AppModule.

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