尚未为模型"ResourceSchema"注册架构. -嵌套js [英] Schema hasn't been registered for model "ResourceSchema". - nest js
问题描述
大家好,我正在尝试使用Nest js和mongodb构建api.
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)
我的RoleSchema
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}],
});
我的ResourceSchema
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);
我的角色service.ts 填充资源数组
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});
}
}
我的存储模块
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?????
感谢andvaced
Thanks in andvaced
推荐答案
ResourceSchema
是架构,而不是模型.您需要为模型注册它,例如:
ResourceSchema
is schema, not model. You need to register it for model, example:
export const Resource = mongoose.model('Resource', ResourceSchema);
更改参考:
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 });
希望有帮助.
这篇关于尚未为模型"ResourceSchema"注册架构. -嵌套js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!