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?

Using new worked for me. This is how I did it. Try it.
const newCollection = await new this.collectionModel(collectionDto);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With