Nestjs:类型不满足约束 'Document'
Nestjs: Type does not satisfy the constraint 'Document'
我收到 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
从 mongoose
导出 Document
然后您将必须将 IBranches
接口扩展到 Document
。如下图-
import { Document } from 'mongoose';
export interface IBranches extends Document{
profileId: string;
name: string;
location: string;
}
我收到 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
从 mongoose
导出 Document
然后您将必须将 IBranches
接口扩展到 Document
。如下图-
import { Document } from 'mongoose';
export interface IBranches extends Document{
profileId: string;
name: string;
location: string;
}