数据没有被推送到主模式数组猫鼬中
Data not getting pushed in main schema array mongoose
我正在尝试将子架构数据推送到用户架构,但它没有被推送到父架构中定义的数组中。
这是我的子控制器代码:-
userr.save((err, doc) =>{
if(!err){
if(req.userData.role2 === 'admin') {
console.log("saving successful");
res.send(doc);
Admin.findOneAndUpdate({ _id: req.userData.userId },{ 'admin.admins' : { $push: { users: userr }}},
function (error, success) {
if (error) {
console.log(error);
}
console.log(success);
});
}
}
父架构:-
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);
所以我想将数据推送到 users
数组中。这里 Userr
是子架构名称 mongoose.model('Userr' , userrSchema);
.
我可以看到在新集合上创建了一个新的子对象,但没有推送到父 Admin
架构。我应该怎么做才能做出改变?
编辑:-
这是我的父架构控制器:-
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.admins' : { $push: { users: userr } } }
。您引用了字段 'admin.admins' 但它不存在,它只是 'admins' 并且我认为语法也不正确。另外我不确定 Mongoose 是否会通过传递整个对象来设置它,但你真正想要插入的是用户 ID,所以我认为你的代码应该是:
Admin.findOneAndUpdate({ _id: req.userData.userId }, { $push: { 'admins.users': userr._id } }, function (error, success) {
//...
});
我正在尝试将子架构数据推送到用户架构,但它没有被推送到父架构中定义的数组中。
这是我的子控制器代码:-
userr.save((err, doc) =>{
if(!err){
if(req.userData.role2 === 'admin') {
console.log("saving successful");
res.send(doc);
Admin.findOneAndUpdate({ _id: req.userData.userId },{ 'admin.admins' : { $push: { users: userr }}},
function (error, success) {
if (error) {
console.log(error);
}
console.log(success);
});
}
}
父架构:-
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);
所以我想将数据推送到 users
数组中。这里 Userr
是子架构名称 mongoose.model('Userr' , userrSchema);
.
我可以看到在新集合上创建了一个新的子对象,但没有推送到父 Admin
架构。我应该怎么做才能做出改变?
编辑:-
这是我的父架构控制器:-
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.admins' : { $push: { users: userr } } }
。您引用了字段 'admin.admins' 但它不存在,它只是 'admins' 并且我认为语法也不正确。另外我不确定 Mongoose 是否会通过传递整个对象来设置它,但你真正想要插入的是用户 ID,所以我认为你的代码应该是:
Admin.findOneAndUpdate({ _id: req.userData.userId }, { $push: { 'admins.users': userr._id } }, function (error, success) {
//...
});