This is the code that i have written,
function getAllTasksToThatProjectType(typeId){
let storeTaskIds = [];
let projectTypeTasks = [];
let returnVar = new Array();
ProjectType.findOne({ _id: typeId })
.exec((error, projectType) => {
// if(error) return res.status(400).json({ error })
if(projectType){
projectType.tasks.map(tsk => storeTaskIds.push(tsk.taskId))
ProjectTask.find({ })
.exec((error, projectTask) => {
// if(error) return res.status(400).json({ error })
if(projectTask){
storeTaskIds.forEach(function(tID){
projectTask.forEach(function(tsk){
if(JSON.stringify(tsk._id) == JSON.stringify(tID) || JSON.stringify(tsk.parentId) == JSON.stringify(tID))
projectTypeTasks.push(tsk)
})
})
// Here would be the api return statement.
return projectTypeTasks;
}
})
}
})
}
const taskForPType = getAllTasksToThatProjectType(req.body.typeOfProject)
If i call the function like this, it will set the value of taskForPType
to undefined
.
I have to do this without a callback function. Any suggestions on how i could i achieve it.