所以我用ClientShema创建了模型客户端,现在我正在创建模型公司。其中一个字段应该是Client类型,我试图这样做,但它似乎崩溃了
这是一些代码:
const userSchema = new mongoose.Schema({
email: {
type: String,
},
username: {
type: String,
required: false
},
createdAt: {
type: Date,
required: true
},
_id: {
type: String,
required: false,
}
}, { collection: 'User' });
const User = mongoose.model('User', userSchema);
这是我的文件,我想使用之前提供的模型。
const client = require('./client');
const companySchema = new mongoose.Schema({
_id: {
type: String,
required: true
},
logo: {
type: String,
required: false
},
companyName: {
type: String
},
clients: {
type: [client.Client]
}
}, { collection: 'Company' });
const Company = mongoose.model('Company', companySchema);
欢迎来到StackOverflow
const client = require('./client');// need to include the schema file
var ClientSchema = mongoose.model('Client').schema;// 'Client' is name of Schema
const companySchema = new mongoose.Schema({
_id: {
type: String,
required: true
},
logo: {
type: String,
required: false
},
companyName: {
type: String
},
clients: {
type: [ClientSchema]
}
}, { collection: 'Company' });
const Company = mongoose.model('Company', companySchema);
希望这能解决你的问题。