我开始使用 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)