Schema hasn't been registered for model “ResourceSchema”. - nest js

╄→尐↘猪︶ㄣ 提交于 2020-05-28 07:01:55

问题


Hello everyone i am trying to build api with Nest js and mongodb.

i am trying to make relationship between schema and i get that Error when i am trying to populate resource from role

[Nest] 12308   - 2019-08-09 4:22 PM   [ExceptionsHandler] Schema hasn't been registered for model "ResourceSchema".
Use mongoose.model(name, schema) +6998ms
MissingSchemaError: Schema hasn't been registered for model "ResourceSchema".
Use mongoose.model(name, schema)

my RoleSchema

import * as mongoose from 'mongoose';
import {ResourceModel} from './resourceSchema';

const Schema = mongoose.Schema;

export const RoleSchema = new Schema({
  name: {
    type: String,
    unique: true,
    required: [true, 'Role name is required'],
  },
  resources: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'ResourceModel',
  }],
  permissions: [{type: String}],
});

my ResourceSchema

import * as mongoose from 'mongoose';

const Schema = mongoose.Schema;

export const ResourceSchema = new Schema({
  name: {
    type: String,
    unique: true,
    required: [true, 'Role type is required'],
  },
  routingInfo: {type: String},
  iconName: {type: String},
  iconType: {type: String},
  subResources: [{type: mongoose.Schema.Types.Mixed, ref: 'ResourceModel'}],
});

export const ResourceModel = mongoose.model('Resource', ResourceSchema);

my Role service.ts populate resources array

...

@Injectable()
export class RoleService {
  constructor(@InjectModel('roles') private readonly roleModel: Model<Role>) {
  }

  async findAll(): Promise<Role[]> {
    return await this.roleModel.find().populate({path: 'resources', Model: ResourceModel});
  }

// also tried that
 async findAll(): Promise<Role[]> {
    return await this.roleModel.find().populate({path: 'resources', Model: ResourceSchema});
  }
}

my resorce mdule

import {Module} from '@nestjs/common';
import {ResourcesController} from './resources.controller';
import {ResourcesService} from './resources.service';
import {MongooseModule} from '@nestjs/mongoose';
import {ResourceSchema} from '../schemas/resourceSchema';
import {ConfigService} from '../config/config.service';
import {AuthService} from '../auth/auth.service';
import {UsersService} from '../users/users.service';
import {UserSchema} from '../schemas/userSchema';

@Module({
  imports: [MongooseModule.forFeature([{name: 'users', schema: UserSchema}]),
    MongooseModule.forFeature([{name: 'resources', schema: ResourceSchema}])],
  providers: [ResourcesService, UsersService, AuthService, ConfigService],
  controllers: [ResourcesController],
  exports: [ResourcesService],
})
export class ResourcesModule {
}

so when i do GET REQUEST on postman i get that error anyone can tell what i am doing wrong?????

Thanks in andvaced


回答1:


ResourceSchema is schema, not model. You need to register it for model, example:

export const Resource = mongoose.model('Resource', ResourceSchema);

Change the ref:

subResources: [{type: mongoose.Schema.Types.Mixed, ref: 'Resource'}],

Them import it to your service and call:

 return await this.roleModel.find().populate({path: 'resources', Model: Resource });

Hope it help.



来源:https://stackoverflow.com/questions/57431221/schema-hasnt-been-registered-for-model-resourceschema-nest-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!