我尽我所能,每一个"解决方案"我发现要么是错误的架构名称,要么是缺失/错误的要求,我很确定这不是我遇到的。
我正在使用Typescript 2.6.2,Node 8.9.4和Mongoose 5.0.2。我有两个型号。一个用于帐户,一个用于组织。当我使用针对Accounts模型的填充运行findOne时,我得到:' MissingSchemaError:Schema尚未注册模型" 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
答案 0 :(得分:0)
我遇到了同样的问题,我猜这是因为你的未使用的导入被编译器删除了。
尝试import './Organization.model';