你好,我是新来的,却没有办法找到解决方法。
我在同一模型文件中有一个主模式和一个用户模式。首先,我正在注册主要模式。注册主架构后,我要注册用户架构。注册将由主要模式用户( admin )完成。因此,我还为 admin 创建了登录名并创建了JWT访问系统。通过同一系统,我现在要注册 users 并将其推送到主模式中提到的数组上。
我为用户架构创建了单独的控制器,但它正在mongodb上创建了一个新集合。
如何推送到同一文档的对象数组?
这是模型:-
var userrSchema = new mongoose.Schema({
email : {
type: String,
required: "Email can't be empty.",
// unique: true
},
password: {
type: String,
required: "Password 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."
},
verify: String,
role: String,
emailResetTokennn: String,
emailExpiress: Date,
saltSecret: String
});
mongoose.model('Userr' , userrSchema);
//define admin(main) schema
var adminSchema = 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.",
},
admins: {
_id: mongoose.Schema.Types.ObjectId,
email : {
type: String,
required: "Email can't be empty.",
unique: true
},
password: {
type: String,
required: "Password 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: String,
role: String,
emailResetTokenn: String,
emailExpires: Date,
saltSecret: String,//this is user for encryption and decryption of password
users:[ { type: mongoose.Schema.Types.ObjectId, ref: 'Userr' } ]
}
});
mongoose.model('Admin', adminSchema);
编辑:-添加了主控制器:-
module.exports.registerAdmin = (req, res, next) =>{
var admin = new Admin();
admin.companyName = req.body.companyName;
admin.address = req.body.address;
admin.contactDetails = req.body.contactDetails;
admin.admins = {
email : req.body.email,
password: req.body.password,
firstName : req.body.firstName,
lastName : req.body.lastName,
phoneNumber : req.body.phoneNumber,
designation : req.body.designation,
role : "admin",
verified :"false",
users: []
};
admin.save((err, doc) =>{