【问题标题】:The 2nd parameter to `mongoose.model()` should be a schema or a POJO`mongoose.model()` 的第二个参数应该是模式或 POJO
【发布时间】:2021-03-15 14:14:57
【问题描述】:

我尝试启动我的应用程序。我什至不知道哪个文件导致了这个问题。 所以说我应该给你看哪个文件。

这里是 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

【问题讨论】:

    标签: mongodb typescript mongoose nestjs


    【解决方案1】:

    我在 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}]),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 2011-08-21
      • 1970-01-01
      • 2020-12-26
      • 2018-10-23
      相关资源
      最近更新 更多