繁体   English   中英

即使包含了架构,.populate上的猫鼬MissingSchemaError

[英]Mongoose MissingSchemaError on a .populate even though the schema is included

我已竭尽所能,发现的每个“解决方案”要么是模式名称错误,要么是缺少/排序错误的需求,我可以肯定这不是我遇到的问题。

我正在使用Typescript 2.6.2,Node 8.9.4和Mongoose 5.0.2。 我有两个模型。 一种是用于帐户,另一种是用于组织。 当我对Accounts模型运行带有填充的findOne时,我得到:'MissingSchemaError:模式尚未为模型“ Organization”注册。

/src/models/Organization.model.ts

import * as mongoose from 'mongoose';

export type OrganizationModel = mongoose.Document & {
   name: string,
   suborganizations: [OrganizationModel]
};

const OrganizationSchema = new mongoose.Schema({
    name: {type String, required: true},
    suborganizations: [
        {type: mongoose.Schema.Types.ObjectId, ref: 'Organization'}
    ]
});

const Organization = mongoose.model<OrganizationModel>('Organization', OrganizationSchema);
export default Organization;

/src/models/Account.model.ts

import * as mongoose from 'mongoose';
import { default as Organization, OrganizationModel } from './Organization.model';

export type AccountModel = mongoose.Document & {
    username: string,
    organization: OrganizationModel
};

const Account = new mongoose.Schema({
    username: {type: String, required: true},
    organization: {type: mongoose.Schema.Types.ObjectId, ref: 'Organization'}
});

const Account = mongoose.model<AccountModel>('Account', AccountSchema);
export default Account;

/src/controller/account.ts

import * as mongoose from 'mongoose';
// I've tried without and with the following import
import { default as Organization, OrganizationModel } from '../models/Organization.model';
import { default as Account, AccountModel } from '../models/Account.model';

const AccountManager = {
    getAccount: async ({ username }) => {
        // Here is the troubled line
        const account = await Account.findOne({ username }).populate('organization organization.suborganizations');
        return account;
    }
};

export default AccountManager

我有同样的问题,我想这是因为编译器删除了您未使用的导入。

尝试import './Organization.model';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM