Reputation: 1220
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 Creator
entity 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
});
Upvotes: 1
Views: 2968
Reputation: 3111
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();
});
});
Upvotes: 1