_id在我的嵌套应用程序中,我在调用时遇到类型错误user,因为猫鼬自动定义了_id,因此它不存在于我的模式中,该模式被定义为承诺的类型。
当承诺类型更改为任何类似类型Promise<any>时,就不会出现类型错误。
async create(createUserDto: CreateUserDto): Promise<User> {
const createdUser = await new this.userModel(createUserDto).save();
return createdUser;
}
Run Code Online (Sandbox Code Playgroud)
但我想知道这是正确的方法还是我应该做其他事情。
我不想_id在架构中定义来解决这个问题。
@Prop({ auto: true})
_id!: mongoose.Types.ObjectId;
Run Code Online (Sandbox Code Playgroud)
用户架构.ts
// all the imports here....
export type UserDocument = User & Document;
@Schema({ timestamps: true })
export class User {
@Prop({ required: true, unique: true, lowercase: true })
email: string;
@Prop()
password: string;
}
export const UserSchema = SchemaFactory.createForClass(User);
Run Code Online (Sandbox Code Playgroud)
用户.controller.ts
@Controller('users')
@TransformUserResponse(UserResponseDto)
export class UsersController {
constructor(private …Run Code Online (Sandbox Code Playgroud)