This expression is not constructable. Type 'xxxx' has no construct signatures

1.5k views Asked by At

I have a mistake that I can't understand what I'm doing wrong. I am following the instructions set out at https://docs.nestjs.com/techniques/mongodb. The difference is that I created interfaces to build different strategies for implementing a service (and for that I am making a wrapper in Model ).

import { Inject, Injectable } from "@nestjs/common";
import { Model } from "mongoose";
import { Documents } from "src/domain/schemas/documents.schema";

interface IDocumentRepository {
  save();
}

interface IDocumentRepositoryFactory {
  new(doc?: any): IDocumentRepository;
}

export function createIDocumentRepository(ctor: IDocumentRepositoryFactory, doc?: any): IDocumentRepository {
  return new ctor(doc);
}

@Injectable()
export class DocumentRepository implements IDocumentRepository {

  constructor(
    private doc?: Documents,
    @Inject()
    private repository?: Model<Documents>
  ) {}

  /* others fields and methods */

  save() {
    this.doc.save()
  }
}

In others code points I call:

someMethod(repository: DocumentRepository) {
    /* others codes */
    const mdoc = new this.repository(mongoModelDoc); // <<<<---- error  
    mdoc.save();
    /* others codes */
}

It is causing the following error:

This expression is not constructable. Type 'DocumentRepository' has no construct signatures.
      return new this.repository(document)

What is wrong and how to resolve to meet this implementation?

0

There are 0 answers