Nestjs: тип не удовлетворяет ограничению «Документ»

#typescript #mongoose #nestjs #document

#typescript #мангуст #nestjs #документ

Вопрос:

Я получаю 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 amp; 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;
}
  

Изображение ошибки

Ответ №1:

Экспортируйте Document , mongoose после чего вам нужно будет расширить IBranches интерфейс Document . Как показано ниже —

 import { Document } from 'mongoose';

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

Комментарии:

1. Пару вещей, которые вы можете попробовать — 1) В ваших службах импортируются ветки из branches.interface.ts. 2) вам также необходимо внести некоторые изменения в schema.ts. Вы можете следить за этим моим репозиторием на github github.com/sprakash57/mow-a-lawn-api для справочных целей. Он полностью работает. Дайте мне знать, если вам нужна помощь.