1

in my company schema i have a posted job which is type array and will hold subdocuments

companySchema.js

PostedJobs : [{
        JobName : { type: String, required : true},
        JobType : { type: String, required : true},
        JobLocation : { type: String, required : true},
        JobSalay: { type: String, required : true}
    }],

in my /company route i get all company registered by specific user through Creatorentity in model

to get that user company i use

  router.get('/',  isLoggedIn ,  function(req, res, next) {
    Company.find({'Creator': req.user.id}).then(function(companies) {
        res.render('Company', { "Companies" : companies });
    });
});

after getting company i want to visit a specific company page on clicking company name(unique)

router.get('/:name' , isLoggedIn , function(req , res , next) {
    var name = req.params.name;
    Company.findOne({Name : name}).then(function(Company) {
         res.render('dashboard',{
            "Company" : Company,
             errors : []
        });
    })
});

now i want to post a job to this specific company from a POST route as my req.body consist of JobName , JobType , JobLocation and JobSalary which i have assigned to a specific variable now how should i push this doc to array

POST route

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;
    //push this job to that specific comapny
});

1 Answer 1

1

I don't know the schema of your company, but if you want to add PostedJobs to the companies, you should define an array field in it.

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;
    //push this job to that specific comapny
    // create the postedJob object
    var postedJob = {JobName : JobName, JobType : JobType, JobLocation : JobLocation, JobSalay:Salary};
    // find the company in DB and add the postedJob to its array of postedJobs
    var name = req.params.name;
    Company.findOne({Name : name}).then(function(company) {
        //modify and save the object received via callback
        company.postedJobs.push(postedJob);
        company.save();
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

i had postedJobs of type array which is
PostedJobs : [{ JobName : { type: String, required : true}, JobType : { type: String, required : true}, JobLocation : { type: String, required : true}, JobSalay: { type: String, required : true} }],

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.