在猫鼬的单个控制器上的多个模型值

时间:2019-05-20 07:47:58

标签: node.js express mongoose mongoose-schema

我为嵌套类型架构创建了3个模型。现在我只想在控制器中保存两个模块详细信息。我该怎么办。

型号:-

//用户模型

protected

//管理员模型

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

1 个答案:

答案 0 :(得分:1)

:尝试以下操作:

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;
            company.admin=[{
                           email : req.body.email;
                           firstName : req.body.firstName; 
                           lastName : req.body.lastName;
                           phoneNumber : req.body.phoneNumber;
                           designation : req.body.designation;
                           role : "admin";
                           id : req.body._id;  
                            }]    
    }admin.save((err, doc) => {})