"Error encountered: 'Callable function cannot be invoked on Mongoose model

In my Nest JS service, the code structure is as follows:

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();
  }
}

Although the code works fine, I encountered a tslint warning ts(2348). Can anyone suggest an alternative approach to resolve this issue without using the // @ts-ignore rule?

https://i.stack.imgur.com/5ZLQY.png

Answer №1

If you're facing this issue, give the new keyword a try like I did. Here's my solution for your reference.

const newObj = await new this.objectModel(objectDto);

Answer №2

To implement this, consider using the code snippet new newCollection.save();. Keep in mind that the schema functions as a constructor.

This technique has been successful in Node.js and should be applicable in React as long as both platforms are utilizing Mongoose.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Designing a user interface for unlimited nested components with optional properties

I am currently working on creating an interface for the specific object type shown below. "Condition": { "And": { "Or": { "userData": [ { "name": "Alex", &q ...

Utilizing TypeORM to selectively choose data in OneToMany relationships

I am looking to create a TypeORM query that pulls data from the database. Specifically, I want to retrieve all clients who have made a purchase but have not initiated a return. Here is the structure of the database: Clients: Id (Int, primary column) Purc ...

Streaming an image object in Sinatra: A beginner's guide

I successfully stored images in mongoDB using GridFS. Now, I am looking to fetch an image from the database and showcase it on a web browser. Is there a way to achieve this by utilizing Sinatra along with HAML? ...

I am attempting to make the fade in and out effect function properly in my slideshow

I've encountered an issue where the fading effect only occurs when the page initially loads and solely on the first image. Subsequently, the fading effect does not work on any other images displayed. This is the CSS code I have implemented by adding ...

Establishing the types of object properties prior to performing a destructuring assignment

Consider a scenario where a function is utilized to return an object with property types that can be inferred or explicitly provided: const myFn = (arg: number) => { return { a: 1 + arg, b: 'b' + arg, c: (() => { ...

Invoking a function on an object of a subclass that derives from an abstract class

In the realm of abstract classes, behold this one: export abstract class BaseStepComponent { /** Behold thy base-step ctor */ constructor() { } abstract getValue(): string; } And lo, here is a component that inherits such abstract glory ...

When utilizing Azure Function, the Mongoose .select function appears to be unavailable

I have been trying to implement the mongoose .select operator in my Azure function, but I keep encountering an error message that says TypeError: db.collection(...).findOne(...).select is not a function at db.collection.find.toArray. Although the user&apos ...

Troub3leshooting Circular Dependency with Typescript, CommonJS & Browserify

I am currently in the process of transitioning a rather substantial TypeScript project from internal modules to external modules. The main reason behind this move is to establish a single core bundle that has the capability to load additional bundles if an ...

Is the RouterModule exclusively necessary for route declarations?

The Angular Material Documentation center's component-category-list imports the RouterModule, yet it does not define any routes or reexport the RouterModule. Is there a necessity for importing the RouterModule in this scenario? ...

If I include the Next.js Image component within a React hook, it may trigger the error message "Attempting to update state on an unmounted component in React."

My UI layout needs to change when the window width changes. However, when I add an Image (Nextjs component) in my hook, I encounter an error message. I am not sure why adding Image (Nextjs component) is causing this problem. The error message is display ...

Setting up MongoDB to save the database in a particular directory on Ubuntu prior to installation

I have my app running on a separate docker container and I'm looking to configure the mongo dbPath when installing MongoDB on the app's docker. Should I use the mongo command mongod --dbpath [PATH] and then update the mongo.conf file? Another qu ...

Customizing event typings for OpenTok in Typescript

Currently, I am working on integrating chat functionality using the 'signal' events with the OpenTok API. Here is my event listener that successfully receives the signal: // Listen for signal CHAT_MESSAGE sess.on('signal:CHAT_MESSAGE', ...

Looping issue with ForEach in Typscript with Firebase Functions

While browsing through various questions on this topic, I've noticed that the specific answers provided don't quite fit my situation. My query involves converting a Google Firebase RTB datasnapshot into an array of custom objects, each representi ...

Creating an enumeration within a class

I'm encountering difficulties when trying to declare an enum element within a class. Despite attempting various methods to declare the enum, I am unable to make it function properly. Here is the (non-functional) class: export class Device extends El ...

Is it considered poor practice in TypeScript to manually set the type when the type inference is already accurate?

Is it necessary to explicitly set the variable type in TypeScript when it is inferred correctly? For example: const add = (a: number, b: number) => a + b; const result = add(2, 3); // Or should I explicitly declare the return value type? const add = ...

Designate as a customizable class property

I'm struggling to create a versatile inheritance class for my services. Currently, I have two service classes named Service_A and Service_B that essentially do the same thing. However, Service_A works with Class_A while Service_B works with Class_B. T ...

Issue when trying to use both the name and value attributes in an input field simultaneously

When the attribute "name" is omitted, the value specified in "value" displays correctly. However, when I include the required "name" attribute to work with [(ngModel)], the "value" attribute stops functioning. Without using the "name" attribute, an error ...

The given 'FC<ComponentType>' type argument cannot be assigned to the 'ForwardRefRenderFunction<unknown, ComponentType>' parameter type

Currently, I am using react in conjunction with typescript. Within my project, there are two components - one serving as the child and the other as the parent. I am passing a ref to my child component, and within that same child component, I am binding my ...

Utilize prop-types inheritance when a component is rendered via props

Is it possible to inherit prop-types when a component is rendered via the parents prop, without direct access to 'ChildProps' and 'Props' interface? Parent Component interface ChildProps { counter: number; setCounter: React.Dispat ...

Struggling to grasp the concept of collections in MeteorJS

Just starting out with MeteorJS and working on an app that generates points on a Google Map (using their API) based on data from an XML api service. When you click on one of these points, detailed information will be displayed. Clear enough. The challenge ...