Creating a job portal website using react and mongodb. A user logins and save the job. So a model is created to save the jobs and created Controller. Before saving a job checks if it is exist or not by passing the _id which fetched from the req.params and the userId which from the req.payload . But it doesn’t checks the existingJob and creating it as a new object.
exports.savedJobsController = async (req,res) =>{
console.log("Inside Saved Jobs Controller");
const {title,salary,email,company,location,description,category,jobType,experience,vacancy,deadline} = req.body
const {id} = req.params
const userId = req.payload
console.log(user id is : ${userId} and id is ${id});
try {
const existingJob = await savedjobs.findOne({_id:id,userId})
console.log(exist ${existingJob});
if(existingJob){
res.status(406).json("Job Already Saved to Your Collection!")
}else{
const newJob = new savedjobs({
title,salary,email,company,location,description,category,jobType,experience,vacancy,deadline,userId
})
await newJob.save()
res.status(200).json(newJob)
console.log(Saved job is
${newJob}
);
}
} catch (error) {
res.status(401).json(error)
console.log(error);
}
}