When calling `mongoose.model()`, the second argument must either be a schema or a plain old JavaScript object (POJO)

Trying to launch my application but unsure which file is causing the issue. Can someone point me in the right direction?

Here is a snippet of my app.module.ts file:

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AuthModule } from './auth/auth.module';
import { AssigmentsModule } from './posts/assigments.module';
import { UsersModule } from './users/users.module';
import { MongooseModule } from '@nestjs/mongoose';

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: '.env',
      isGlobal: true,
    }),
    MongooseModule.forRoot(process.env.MONGO_URI),
    AuthModule,
    AssigmentsModule,
    UsersModule,
  ],
  controllers: [AppController],
})
export class AppModule {}

Additionally, here is a link to the repository with the code on the dev branch: https://github.com/MoneyIgos/biuro-makowskaj-api/tree/dev

Answer №1

Upon reviewing your code on Github, it appears that you may have already resolved the issue. I encountered a similar problem recently and here is how I tackled it:

The User Schema is structured as follows:
    export const UserSchema = new Schema({
      username: { type: String, required: true },
      password: { type: String, required: true },
    });

const User = model<IUser>('User', UserSchema);

For the Assignment Schema, the format looks like this:

 export const AssigmentSchema = new Schema({
  title: { type: String, required: true },
  description: { type: String, required: true },
  image: { type: String, required: false },
  createdAt: {
    type: String,
    default: () => new Date(),
  },
});

const Assigment = model<IAssigment>('Assigment', AssigmentSchema);

Correction needed in UserModule.ts:

  imports: [MongooseModule.forFeature([{ name: 'User', schema: UserSchema }])],

Change it to:

  imports: [MongooseModule.forFeature([{ name: 'User', schema: User}])],

In AssignmentModule.ts:

MongooseModule.forFeature([{ name: 'Assigment', schema: AssigmentSchema }]),

It should be modified to:

MongooseModule.forFeature([{ name: 'Assigment', schema: Assignment}]),

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

An error is triggered when using db.listCollections() in an ExpressJS environment

I am currently working with ExpressJs and MongoDB, but I've encountered an issue with the following code: var db = monk(DB_URL); app.use(function (req, res, next) { req.db = db; next(); }); app.get("/view_collections", function (req, res) { ...

Transformation of Ionic 2 ScreenOrientation Plugin

Can someone assist me with this issue? A while back, my Ionic 2 app was functioning correctly using the ScreenOrientation Cordova plugin and the following code: window.addEventListener('orientationchange', ()=>{ console.info('DEVICE OR ...

Error: Unable to access the 'secondary' property of an undefined object - encountered after updating Material-UI from version 4 to version 5

We recently completed an upgrade from MUI version 4 to version 5, and since the upgrade, our UI tests have been failing with the following error: TypeError: Cannot read property 'secondary' of undefined (I added comment to which line in code thi ...

Make sure the auto import feature in TypeScript Visual Studio Code Editor is set to always use the ".js" extension

At times, the auto-import completion feature includes the .js extension, but inconsistently. When this extension is missing in the TypeScript source, the emitted JavaScript file may encounter runtime issues like module not found error since the tsc compile ...

mongoose: uniqueness constraint not being enforced

I am currently seeking information on how to ensure uniqueness for a field that is not an index. I have come across a similar question here, but the solution of using dropDups: true has been deemed outdated. What is the proper method for enforcing unique ...

Tips for implementing Peer.js in Next.js using TypeScript?

Next.js has the ability to run on the server side, which can result in Peer.js throwing errors when used with Next.js. One user on Stack Overflow explains this issue: The error is likely due to peer js performing side effects during import. The suggest ...

The "rest" variable is automatically assigned the type of "any" because it lacks a specified type and is used within its own initializer

Attempting to set up a private route using react router 4 and Typescript. Check out the code I'm working with: type CustomRouteProps<T> = T & { component: any, authRequired: boolean }; function PrivateRoute({ component: Component, authRequ ...

Storing data in MongoDB in separate locations

As a newcomer to MongoDB, I'm encountering issues with locating and accessing the data I've created or imported, as it seems to be stored in two separate locations. When I start the MongoDB shell using the command: $ mongo and then execute: ...

Converting strings to integers using the Remote API in MongoLab

Is there a way in which I can convert a specific field from a string to an integer without having to update all existing values with an API call for each document? Can MongoLab update values using the existing value in an update call, similar to how you ...

Encountered an error while attempting to load the user into the session

Testing my authentication, I randomly created a user with the login 8===D and password 123. However, when I tried to log in, passportjs failed to serialize that user. But when I used a regular username like [email protected], it worked fine and serial ...

Creating fixtures with Playwright is a simple process that can greatly enhance

I have a requirement to set up fixtures where the first fixture is always available (acting as a base class) and the second fixture will vary in different test files (like a derived class). I've implemented the following code which seems to be working ...

Generating outcomes of a particular shape using a MongoDB query/aggregation pipeline

Imagine this scenario: const user = { firstname: { type: String, default: '' }, lastname: { type: String, default: '' }, goals: { type: Number, default: 0 }, }; Now consider this group of data: [{ id: 1, firstname: 'mary ...

Error: The lockfile and package.json file are not synchronized when running npm

Having a problem with NPM where the package-lock and package.json files are out of sync. Tried deleting node_modules, running npm install, but issue persists. Any suggestions? Error: npm ci can only install packages when package.json and package-lock.json ...

What is the correct way to specify the type in my functional component?

I was trying to define the type in my component in a different way, I know it can be done using classes but is there a way to achieve this with functional components without exporting the interface? Despite my extensive research, I couldn't find any r ...

A step-by-step guide on importing stompjs with rollup

My ng2 app with TypeScript utilizes stompjs successfully, but encounters issues when rollup is implemented. The import statement used is: import {Stomp} from "stompjs" However, upon running rollup, the error "EXCEPTION: Stomp is not defined" is thrown. ...

Looking to effortlessly move and arrange items with ng2-drag-drop in Angular 2?

I have encountered a problem with the ng2-drag-drop library. I am trying to drag and drop items from one div to another, and then freely move the dropped item within the droppable area. One issue I'm facing is that when I drop my draggable item in th ...

TypeScript Interfaces: A Guide to Defining and

In my angular2 application, I have created interfaces for various components. One of these interfaces is specifically for products that are fetched from a remote API. Here is the interface: export interface Product { id: number; name: string; } ...

Querying Records between Two Dates in MongoDB with CodeIgniter

I am currently working on retrieving records between two specific dates by utilizing the CodeIgniter framework in conjunction with MongoDB. I have managed to fetch records successfully, but there seems to be an issue with the accuracy of the results. Here ...

Steering clear of Unique error E11000 through efficient handling with Promise.all

In my development work, I have been utilizing a mongoose plugin for the common task of performing a findOrCreate operation. However, I recently discovered that running multiple asynchronous findOrCreate operations can easily result in an E11000 duplicate k ...

Enhancing NodeJS performance when manipulating arrays

I'm seeking a way to retrieve a user's chat history with other users from a separate collection in NodeJS and MongoDB. I have concerns about the potential performance impact of running the code below due to the nature of NodeJS. While I could d ...