Reputation: 91
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?
Upvotes: 3
Views: 1088
Reputation: 33
Using new
worked for me. This is how I did it. Try it.
const newCollection = await new this.collectionModel(collectionDto);
Upvotes: 2
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