小编kos*_*smo的帖子

NestJS/Mongoose:序列化不排除普通输出中的属性

我开始使用 NestJS,从旧的express/mongoose 项目迁移,并立即撞上了栅栏,只是遵循 NestJS 文档中的 MongoDB/序列化章节。我准备了以下架构

/////// schema
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import * as mongoose from 'mongoose';
import { Exclude, Expose } from 'class-transformer';

export type UserDocument = User & mongoose.Document;

@Schema()
export class User {
    @Prop()
    @Exclude()
    _id: String

    @Expose()
    get id(): String { return this._id ? `${this._id}` : undefined }

    @Prop()
    name: string

    @Prop({ unique: true })
    login: string

    @Exclude()
    @Prop()
    password: string        
}

export const UserSchema = SchemaFactory.createForClass(User);
Run Code Online (Sandbox Code Playgroud)

将其注册到app.module中

MongooseModule.forRoot('mongodb://localhost/old_project'), 
MongooseModule.forFeature([ { …
Run Code Online (Sandbox Code Playgroud)

javascript nestjs nestjs-mongoose

8
推荐指数
3
解决办法
8885
查看次数

标签 统计

javascript ×1

nestjs ×1

nestjs-mongoose ×1