0

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.

Rusher
  • 1
  • 2
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Krzysztof Krzeszewski Sep 11 '20 at 07:19
  • *"I have to do this without a callback function"* That's probably not possible. There will be a reason why `.exec` accepts a callback. But you should tell us more about what `ProjectType` is. This might help you as well to understand the overall problem you are dealing with: https://felix-kling.de/blog/2019/javascript-callbacks-misconceptions.html – Felix Kling Sep 11 '20 at 07:19

1 Answers1

0

It is returning undefined, because your function hasn't finished when you called. You could rewrite the function asynchronously like this:

async function getAllTasksToThatProjectType(typeId) {
  const storeTaskIds = [];
  const projectTypeTasks = [];
  const returnVar = new Array();
  return new Promise(function (resolv, reject) {
    ProjectType.findOne({ _id: typeId }).exec((error, projectType) => {
      if (error) {
        reject(error);
      }
      if (projectType) {
        projectType.tasks.map((tsk) => storeTaskIds.push(tsk.taskId));
        ProjectTask.find({}).exec((error, projectTask) => {
          if(error) {
            reject(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 resolve(projectTypeTasks);
          }
        });
      }
    });
  });
}

const taskForPType = await getAllTasksToThatProjectType(req.body.typeOfProject);

For more info you could check more info about async calls: How do I return the response from an asynchronous call?

raven
  • 2,381
  • 2
  • 20
  • 44