2

I'm getting Type 'BranchesDocument' does not satisfy the constraint 'Document'. error message. Can anyone please tell what is the mistake in my code?

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

Johnny
  • 261
  • 7
  • 22

1 Answers1

3

Export Document from mongoose then you will have to extend IBranches interface to Document. Like below -

import { Document } from 'mongoose';

export interface IBranches extends Document{
  profileId: string;
  name: string;
  location: string;
}
Sunny Prakash
  • 772
  • 1
  • 8
  • 13
  • A couple of things you can try - 1) In your services import Branches from branches.interface.ts. 2) you need to make some changes to schema.ts too. You can follow this github repo of mine https://github.com/sprakash57/mow-a-lawn-api for reference purposes. It is fully working. Let me know if you need help. – Sunny Prakash Oct 14 '20 at 17:15