简体   繁体   English

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

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

I have dug all I can and every "solution" I find is either a mistyped schema name or a missing / misordered require, which I'm quite certain is not what I'm running into. 我已竭尽所能,发现的每个“解决方案”要么是模式名称错误,要么是缺少/排序错误的需求,我可以肯定这不是我遇到的问题。

I am using Typescript 2.6.2, Node 8.9.4, and Mongoose 5.0.2. 我正在使用Typescript 2.6.2,Node 8.9.4和Mongoose 5.0.2。 I have two models. 我有两个模型。 One is for Accounts and one for Organizations. 一种是用于帐户,另一种是用于组织。 When I run a findOne with a populate against the Accounts model, I get: 'MissingSchemaError: Schema hasn't been registered for model "Organization".' 当我对Accounts模型运行带有填充的findOne时,我得到:'MissingSchemaError:模式尚未为模型“ Organization”注册。

/src/models/Organization.model.ts /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 /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 /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

I had the same problem and i guess this happened because your unused import was removed by compiler. 我有同样的问题,我想这是因为编译器删除了您未使用的导入。

Try import './Organization.model'; 尝试import './Organization.model';

暂无
暂无

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

相关问题 Mongoose 收到错误:MissingSchemaError: Schema has not been registered for model - Mongoose getting the error: MissingSchemaError: Schema hasn't been registered for model throw new mongoose.Error.MissingSchemaError(name) MissingSchemaError: Schema还没有为model“superheros”注册 - throw new mongoose.Error.MissingSchemaError(name) MissingSchemaError: Schema hasn't been registered for model “superheros” ZCCADDEDB567ABAE643E15DCF0974E503Z 错误 MissingSchemaError: Schema 尚未为 typescript 中的 model 注册 - Mongoose error MissingSchemaError: Schema hasn't been registered for model in typescript 填充嵌套模式猫鼬 - Populate in nested schema Mongoose 在3模式上填充mongoose - Populate mongoose on 3 schema MissingSchemaError:尚未为模型“用户,userSchema”注册架构。 使用 mongoose.model(name, schema) - MissingSchemaError: Schema hasn't been registered for model "User, userSchema". Use mongoose.model(name, schema) 猫鼬:填充或引用架构上的 ID? - Mongoose: Populate or reference Id on Schema? ZCCADDEDB567ABAE643E15DCF0974E503Z 如何在数组中填充模式 - Mongoose how to populate schema in array 猫鼬插件抛出错误:MissingSchemaError:尚未为模型“约会”注册架构 - mongoose plugin throws an error: MissingSchemaError: Schema hasn't been registered for model “Appointment” (ESRI) ReferenceError: Locator is not defined - 即使它被包含在内 - (ESRI) ReferenceError: Locator is not defined - Even though it is included
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM