gpt4 book ai didi

node.js - 带有 MongoDB 和 NestJsxAutomapper 的 NestJS 导致错误 'cannot read property plugin of undefined'

转载 作者:行者123 更新时间:2023-12-04 15:20:13 25 4
gpt4 key购买 nike

我正在使用 NestJS 开发 API,并且因为我有 DTO,所以我正在使用 AutoMapper(由 @nartc 和/或 nestjsx 制作),我尝试使用 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 {}

这是我的实体:
// 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);

这是我的 DTO:
// foo.dto.ts
export class FooDTO {
name: string;
}

这是我的 Controller :
// 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);
}
}

如果我取消注释我在模块中突出显示的行,它将导致以下错误..
[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)
我也提到了 this answer on stackoverflow ,但这对我也不起作用。我还将其与 documentation 结合使用, 但没有运气.. 我怎样才能让 AutoMapper 注册我的个人资料?
更新
如果我删除 extends Document,错误似乎源于 foo 实体。和 Schema() , Prop({ ... })从类里面它工作正常,似乎我必须注入(inject) Mongoose 之类的东西?

最佳答案

在您的模块中,只需导入配置文件的路径,如下所示:

import 'relative/path/to/foo.profile';
通过导入文件路径, TypeScript将文件包含在包中,然后是 @Profile()装饰器将被执行。当 @Profile()被执行, AutoMapperModule跟踪所有 Profiles然后轮到 NestJS 初始化 AutoMapperModule (使用 withMapper() 方法), AutoMapperModule将自动将配置文件添加到 Mapper 实例。
话虽如此,在您的 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) {

}
}
以上答案将解决您的问题 AutoMapper .至于你的 Mongoose 问题,我需要一个样本重现来确定。此外,请访问我们的 Discord 以了解此类问题。

关于node.js - 带有 MongoDB 和 NestJsxAutomapper 的 NestJS 导致错误 'cannot read property plugin of undefined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63470364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com