Я создал 3 модели для схемы вложенного типа. Теперь я хочу сохранить только две детали модуля в контроллере. Как я могу это сделать.
Модель: -
// модель пользователя
var userModelSchema = new mongoose.Schema({
email : {
type: String,
required: "Email can't be empty.",
unique: true
},
password: {
type: String,
required: "First name can't be empty."
},
firstName : {
type: String,
required: "First name can't be empty."
},
lastName : {
type: String,
required: "Last name can't be empty."
},
phoneNumber : {
type: String,
required: "Reqired for further contact. Can't be empty."
},
verified: {
type: Boolean,
default: false
},
role: String,
emailResetToken: String,
emailExpires: Date,
saltSecret: String //this is user for encryption and decryption of password
})
mongoose.model('users', userModelSchema ,'users' );
// модель администратора
var adminModelSchema = new mongoose.Schema({
email : {
type: String,
required: "Email can't be empty.",
unique: true
},
password: {
type: String,
required: "First name can't be empty."
},
firstName : {
type: String,
required: "First name can't be empty."
},
lastName : {
type: String,
required: "Last name can't be empty."
},
phoneNumber : {
type: String,
required: "Reqired for further contact. Can't be empty."
},
designation : {
type: String,
required: "Designation can't be empty."
},
verified: {
type: Boolean,
default: false
},
role: String,
emailResetTokenn: String,
emailExpires: Date,
saltSecret: String,
users : [{
type : mongoose.Schema.Type.ObjectId,
ref:'users'
}]
});
// модель компании
var companyModelSchema = new mongoose.Schema({
companyName : {
type: String,
required: "Company name can't be empty.",
required: false
},
companyID: {
type: String,
},
address : {
type: String,
required: "Address can't be empty.",
},
contactDetails : {
type: String,
required: "Company contact number can't be empty.",
},
admin : {
type : mongoose.Schema.Type.ObjectId,
ref:'admin '
}
});
mongoose.model('company',companyModelSchema ,'company');
Итак, теперь я хочу использовать данные администратора и компании для передачи в контроллер, когда пользователь будет пуст []
на данный момент. Как я могу упомянуть 2 модели admin
и company
на одном контроллере?
Для одной модели я не чувствовал проблемы. Как я могу сделать для более чем одной модели?
const mongoose = require ('mongoose');
const users = mongoose.model('users');
const admin = mongoose.model('admin');
const company = mongoose.model('company');
var MongoClient = require('mongodb').MongoClient;
module.exports.registerAdmin = (req, res, next) =>{
var company = new company();
company.companyName = req.body.companyName;
company.address = req.body.address;
company.contactDetails = req.body.contactDetails;
?? admin.email = req.body.email;
?? admin.firstName = req.body.firstName;
?? admin.lastName = req.body.lastName;
?? admin.phoneNumber = req.body.phoneNumber;
?? admin.designation = req.body.designation;
?? admin.role = "admin";
?? admin.id = req.body._id;
}
admin.save((err, doc) => {})