问题描述
我正在使用nestJS开发API,并且由于我具有DTO,因此我正在使用AutoMapper(由@nartc和/或nestjsx制造),因此我尝试将{{1 }}示例,因为我使用了多个文件。
这是我的模块:
Foo
这是我的实体
// foo.module.ts
import { Module } from "@nestjs/common";
import { MongooseModule } from "@nestjs/mongoose";
import { Foo,FooSchema } from "./foo.entity.ts";
import { FooController } from "./foo.controller.ts";
import { FooService } from "./foo.service.ts";
import { FooProfile } from "./foo.profile.ts";
@Module({
imports: [
MongooseModule.forFeature([
{
name: Foo.name,schema: FooSchema,collection: "foos",}
])
// FooProfile <-- if I uncomment this,the program will give the error (shown at the bottom of this question)
],controllers: [FooController],providers: [FooProivder],})
export class FooModule {}
这是我的DTO:
// foo.entity.ts
import { Schema,SchemaFactory,Prop } from "@nestjs/mongoose";
import { Document } from "mongoose";
@Schema()
export class Foo extends Document { // if I remove the `extends Document` it works just fine
@Prop({ required: true })
name: string;
@Prop()
age: number
}
export const FooSchema = SchemaFactory.createForClass(Foo);
这是我的控制者:
// foo.dto.ts
export class FooDTO {
name: string;
}
这是我的个人资料:
// foo.controller.ts
import { Controller,Get } from "@nestjs/common";
import { InjectMapper,AutoMapper } from "nestjsx-automapper";
import { Foo } from "./foo.entity";
import { FooService } from "./foo.service";
import { FooDTO } from "./dto/foo.dto";
@Controller("foos")
export class FooController {
constructor(
private readonly fooService: FooService
@InjectMapper() private readonly mapper: AutoMapper
) {}
@Get()
async findAll() {
const foos = await this.fooService.findAll();
const mappedFoos = this.mapper.mapArray(foos,Foo,FooDTO);
// ^^ this throws an error of the profile being undefined (duh)
return mappedFoos;
}
}
// foo.profile.ts
import { Profile,ProfileBase,InjectMapper,AutoMapper } from "nestjsx-automapper";
import { Foo } from "./foo.entity";
import { FooDTO } from "./foo.dto";
@Profile()
export class FooProfile extends ProfileBase {
constructor(@InjectMapper() private readonly mapper: AutoMapper) {
// I've read somewhere that the `@InjectMapper() private readonly` part isn't needed,// but if I exclude that,it doesn't get the mapper instance. (mapper will be undefined)
super();
this.mapper.createMap(Foo,FooDTO);
}
}
我也提到过this answer on stackoverflow,但这对我也不起作用。我也将其与documentation结合在一起,但是没有运气。我如何让AutoMapper注册我的个人资料?
更新
该错误似乎是由于foo实体引起的,如果我从类中删除了[nest] 11360 - 2020-08-18 15:53:06 [ExceptionHandler] Cannot read property 'plugin' of undefined +1ms
TypeError: Cannot read property 'plugin' of undefined
at Foo.Document.$__setSchema ($MYPATH\node_modules\mongoose\lib\document.js:2883:10)
at new Document ($MYPATH\node_modules\mongoose\lib\document.js:82:10)
at new Foo($MYPATH\dist\foo\foo.entity.js:15:17)
和extends Document
,Schema()
,它就可以正常工作,看来我必须注入猫鼬了还是什么?
解决方法
在您的模块中,只需将路径导入配置文件,如下所示:
import 'relative/path/to/foo.profile';
通过导入文件路径,TypeScript
将把文件包含在包中,然后执行@Profile()
装饰器。执行@Profile()
时,AutoMapperModule
会跟踪所有Profiles
,然后轮到NestJS初始化AutoMapperModule
(使用withMapper()
方法)时,{{1 }}会自动将配置文件添加到Mapper实例。
话虽如此,在AutoMapperModule
的构造函数中,您将获得FooProfile
实例,此配置文件将被添加到
AutoMapper
以上答案将解决您使用@Profile()
export class FooProfile extends ProfileBase {
// this is the correct syntax. You would only need private/public access modifier
// if you're not going to use this.mapper outside of the constructor
// You DON'T need @InjectMapper() because that's Dependency Injection of NestJS.
// Profile isn't a part of NestJS's DI
constructor(mapper: AutoMapper) {
}
}
时遇到的问题。至于您的猫鼬问题,我需要一个样本再现来确定。另外,请访问我们的Discord,以解决此类问题。
什么对我有用。
1.更新了模型、架构、实体的所有绝对路径(如果您在项目中搜索 from '/src
,并将所有路由更新为相对路径,则很容易)
来自:
import { User } from 'src/app/auth/models/user/user.entity';
到:
import { User } from './../../auth/models/user/user.entity';
2.猫鼬进口:
来自:
import mongoose from 'mongoose';
到:
import * as mongoose from 'mongoose';
3.如果不使用验证管道,请删除它。 出于某种原因(我想我没有在控制器上使用它们,我没有调查,我已经从一个控制器中删除了验证管道)所以如果你有这个试试:
来自:
@Controller('someroute')
@UsePipes(new ValidationPipe())
export class SomeController {}
到:
@Controller('someroute')
export class SomeController {}
希望我的解决方案对你有用^_^