数据未在主模式数组猫鼬中推送

时间:2019-06-07 06:49:41

标签: node.js mongodb mongoose mongoose-schema

我正在尝试将子模式数据推送到用户模式,但是没有将其推送到父模式中定义的数组中。

这是我的子控制器代码:-

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 : [] 
    };

1 个答案:

答案 0 :(得分:1)

我认为问题出在这里:{ 'admin.admins' : { $push: { users: userr } } }。您引用了“ admin.admins”字段,但它不存在,只是“ admins”,我认为语法也不正确。另外,我不确定猫鼬是否可以通过传递整个对象来设置好它,但是您真正要插入的是用户ID,所以我认为您的代码应该是:

Admin.findOneAndUpdate({ _id: req.userData.userId }, { $push: { 'admins.users': userr._id } }, function (error, success) {
    //...
});