5

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

Russ Cam
  • 124,184
  • 33
  • 204
  • 266
Erez
  • 574
  • 1
  • 6
  • 23

2 Answers2

6

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.

Cuong Le Ngoc
  • 11,595
  • 2
  • 17
  • 39
1

I had a similar issue with a supporting schema that was never imported into any module. I resolved it by importing that target schema into a random module like so:

imports: [MongooseModule.forFeature([{ name: LoanOfficer.name, schema: LoanOfficerSchema }])]
Dürrani
  • 950
  • 12
  • 21