Как установить пользовательскую модель коллекции как dataType в монго? - PullRequest
1 голос
/ 22 апреля 2019

Итак, я создал модель Client с ClientShema и сейчас я создаю модель Company. Одно из полей должно иметь тип 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);

1 Ответ

0 голосов
/ 22 апреля 2019

Добро пожаловать в 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);

Надеюсь, это решит вашу проблему.

...