wujekkiryl
wujekkiryl

Reputation: 91

mongoose Model is not callable tslint error

I have a simple nest js service which looks like below:

import { Injectable } from '@nestjs/common';
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { Collection } from './interfaces/collection.interface';
import { CollectionDto } from './dto/collection.dto';
import { COLLECTION } from '../constants';

@Injectable()
export class CollectionsService {
  constructor(
    @InjectModel(COLLECTION) private readonly collectionModel: Model<Collection>
  ) {}

  async getAllCollections(): Promise<Collection[]> {
    const collections = await this.collectionModel.find().exec();
    return collections;
  }

  async addCollection(collectionDto: CollectionDto): Promise<Collection> {
    const newCollection = await this.collectionModel(collectionDto);
    return newCollection.save();
  }
}

The code is working well but I am getting tslint warning ts(2348). Does anybody know how to work around it a different way than use // @ts-ignore rule?

screenshot that shows what is happening here

Upvotes: 3

Views: 1088

Answers (2)

Rishabh Singh
Rishabh Singh

Reputation: 33

Using new worked for me. This is how I did it. Try it.

const newCollection = await new this.collectionModel(collectionDto);

Upvotes: 2

Algo7
Algo7

Reputation: 2166

Try return new newCollection.save(); The schema is a constructor.

This works in Node.js, so it should also work in React if we are both usign Mongoose.

Upvotes: 1

Related Questions