使用mongoose将数据推送到嵌入式阵列

时间:2017-12-05 10:03:56

标签: node.js mongodb mongoose

我的架构看起来像这样

var Company ={
    Name 
    Email 
    Location 
    Industry 
    Creator 
    PostedJobs : [{
        JobName 
        JobType 
        JobLocation 
        JobSalary
        Applicants : [{
            Name 
            Status 
        }]
    }]
} 

在这里推送工作是我正在运行的路线代码

router.post('/:name' , isLoggedIn , function(req , res , next) {
    var JobName = req.body.JobName;
    var JobType = req.body.JobType;
    var JobLocation = req.body.JobLocation;
    var Salary = req.body.Salary;
    var postedJob = {JobName : JobName, JobType : JobType, JobLocation : JobLocation, JobSalary:Salary};
    var name = req.params.name;
    Company.findOne({Name : name}).then(function(Company) {
        Company.PostedJobs.push(postedJob);
        Company.save();
        req.flash('sucess_msg' , 'Job Added Sucessfully');
        res.render('dashboard',{
            "Company" : Company
        });
    });
});

现在我想按申请按钮推送申请人 申请代码是

router.get('/:id/apply' , isLoggedIn , function(req , res , next) {
    var appicant = { Name : req.user.FirstName,
            Status : "Applied"
        };
        Company.find({'PostedJobs._id' : req.params.id}).then(function(job) {
        Company.PostedJobs.Applicants.push(applicant);
        Company.save();
        req.flash('sucess_msg' , 'Job Added Sucessfully');
        res.redirect('/jobs');
    });

 });

我也试过

这不起作用,我不知道如何处理这个

1 个答案:

答案 0 :(得分:0)

你能试试吗?推送申请人时。

Company.PostedJobs[0].Applicants.push(applicant);