[英]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.