【问题标题】:Nestjs: Type does not satisfy the constraint 'Document'Nestjs:类型不满足约束“文档”
【发布时间】:2021-01-28 20:14:11
【问题描述】:

我收到Type 'BranchesDocument' does not satisfy the constraint 'Document'. 错误消息。谁能告诉我代码中的错误是什么?

branches.service.ts

import {Injectable} from '@nestjs/common';
import {InjectModel} from '@nestjs/mongoose';
import {Model} from 'mongoose';
import {BranchesDocument} from './branches.schema';

@Injectable()
export class BranchesService {
  constructor(@InjectModel('Branches') private branchesModel: Model<BranchesDocument>) {}
}

branches.schema.ts

import {Prop, Schema, SchemaFactory} from '@nestjs/mongoose';
import {IBranches} from './branches.interface';

export type BranchesDocument = BranchDetails & Document;

@Schema()
export class BranchDetails implements IBranches {
  @Prop()
  profileId: string;

  @Prop()
  name: string;

  @Prop()
  location: string;
}

export const BranchesSchema = SchemaFactory.createForClass(BranchDetails);

branches.interface.ts

export interface IBranches {
  profileId: string;
  name: string;
  location: string;
}

Error image

【问题讨论】:

    标签: typescript mongoose nestjs document


    【解决方案1】:

    mongoose 导出Document 然后您必须将IBranches 接口扩展为Document。如下 -

    import { Document } from 'mongoose';
    
    export interface IBranches extends Document{
      profileId: string;
      name: string;
      location: string;
    }
    

    【讨论】:

    • 您可以尝试几件事 - 1) 在您的服务中,从 branch.interface.ts 导入分支。 2)您也需要对 schema.ts 进行一些更改。您可以关注我的这个 github repo github.com/sprakash57/mow-a-lawn-api 以供参考。它正在完全工作。如果您需要帮助,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 2019-09-19
    • 2020-10-25
    • 2021-04-15
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多