I am fairly new to web development as a whole, so I don't understand promises and async/await much. I'm trying to build a job posting site as a project, but got stuck as I'm unable to make a particular piece of code behave as I want.
In the following snippet, the recruiter who posted a particular job requirement wants to see the skills of the applicant that are relevant to the job requirement. So the code would first get the job details and the details of the applicant in question from the mongodb collection. Then it'll try to first store the required skill of the applicant and then all the skills of the applicant in the skills array. Only after storing all of the above data in skills[], should the code should respond with the last json. But it's not behaving like I want and its responding before the storing is complete. Can someone guide me on how to make it "synchronous"?
NOTE: I removed all the error handling to make the snippet shorter. Also, the applicant has 2 required skills out of overall 5 skills.
router.post('/recruiter/viewApplicant', passport.authenticate('jwt', {session: false}), (req, res, next) => {
if(req.user.accountType === "Recruiter") {
skills = [];
Job.getJobByID((req.body.jobID),(err, job) => {
User.getUserByEmail(req.body.email,(err, user) => {
let i =0;
job.requiredSkills.requirements.forEach(reqSkill => {
user.classData.forEach(classDetail => {
if(classDetail.classID === reqSkill.class.replace(/ /g, '-')) {
ClassData.getClassByclassID(classDetail.classID,(err, classInfo) => {
skills.push({
type: 'req',
imgURL: classInfo.imgURL,
skill: classDetail
})
console.log('req: '+i)
i++
})
}
})
})
let k=0
user.classData.forEach(classDetail => {
ClassData.getClassByclassID(classDetail.classID,(err, classInfo) => {
skills.push({
type: 'all',
imgURL: classInfo.imgURL,
skill: classDetail
})
console.log('all: '+k)
k++
})
})
})
console.log("Completed");
res.json({success: true, msg: "Successful", data: skills});
})
}
});
Expected Result:
req: 0
req: 1
all: 0
all: 1
all: 2
all: 3
all: 4
Completed
Actual Result:
Completed
req: 0
req: 1
all: 0
all: 1
all: 2
all: 3
all: 4