mongodb - `mongoose.model()` 的第二个参数应该是模式或 POJO
问题描述
我试图启动我的应用程序。我什至不知道哪个文件导致了这个问题。所以说我应该给你看哪个文件。
这是 app.module.ts
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 {}
这是这个 repo 看看 dev 分支。 https://github.com/MoneyIgos/biuro-makowskaj-api/tree/dev
解决方案
我在 github 上看到了你的代码,也许你已经想通了,几分钟前我也遇到了同样的问题,这就是我解决的方法
User Schema Looks Like this
export const UserSchema = new Schema({
username: { type: String, required: true },
password: { type: String, required: true },
});
const User = model<IUser>('User', UserSchema);
分配模式看起来像这样
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);
在 UserModule.ts
imports: [MongooseModule.forFeature([{ name: 'User', schema: UserSchema }])],
相反,它应该是
imports: [MongooseModule.forFeature([{ name: 'User', schema: User}])],
在 AssignmentModule.ts
MongooseModule.forFeature([{ name: 'Assigment', schema: AssigmentSchema }]),
应该是这样的
MongooseModule.forFeature([{ name: 'Assigment', schema: Assignment}]),