mongoose 无法访问nestjs中的嵌套属性-类型“()=> Company”上不存在属性“islogs”

cl25kdpy  于 11个月前  发布在  Go
关注(0)|答案(1)|浏览(143)

company.entity.ts

@Schema({
  timestamps: true,
  versionKey: false,
})
export class Company {
  @Prop({ type: String, trim: true, required: true })
  name: string;

  @Prop({ type: String, trim: true, required: true })
  address: string;

  @Prop({ type: Boolean, required: true, default: false })
  isDeleted: boolean;

  @Prop({
    type: mongoose.Schema.Types.ObjectId,
    required: true,
    ref: 'User',
  })
  createdBy: () => User;
}

export const CompanySchema = SchemaFactory.createForClass(Company);

字符串

user.schema.ts

@Schema({
  timestamps: true,
  versionKey: false,
})
export class User {
  @Prop({
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Company',
    required: true,
  })
  companyId: () => Company;

  @Prop({ type: String, trim: true, required: true })
  firstName: string;

  @Prop({ type: String, trim: true, required: true })
  lastName: string;

  @Prop({ type: String, trim: true, required: true, unique: true })
  email: string;

  @Prop({ type: String, trim: true })
  password: string;

  @Prop({ type: Boolean, required: true, default: false })
  isDeleted: boolean;

}

export const UserSchema = SchemaFactory.createForClass(User);


现在,当我试图访问嵌套的属性,我从用户数据使用findOne

const user = await this.userModel
    .findOne(
      { _id: payload.id, isDeleted: false, isActive: true },
    )
    .populate('companyId')
    .lean();

  if (user.companyId.isDeleted && user.companyId.isDeleted === false)
    throw new UnauthorizedException(CONSTANT.NOT_FOUND('User'));


它给予我一个 typescript 错误,
类型“()=> Company”上不存在属性“”islogic“。
我如何解决这个typescript问题。

qmelpv7a

qmelpv7a1#

你试过@Prop({ type: Boolean, required: true, default: false }) public isDeleted: boolean;

相关问题