猫鼬中单个控制器上的多个模型值
Multiple model values on a single controller in mongoose
我已经为嵌套类型模式创建了 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) => {})
:试试这个:
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) => {})
我已经为嵌套类型模式创建了 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) => {})
:试试这个:
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) => {})