0

I'm running a Node Js server and I'm receiving the following content from my frontend:

{
  dataType: "any",
  fechaI: "any",
  fechaF: "any",
  checkedList: [ "any obj","any obj"]
}

I have to iterate over the checkedList array in order to get the action property of all of its objects and then make use of the import for queries

const query = require("./queries.js")

query[item.action](req,res,resp,function(err,data){
            console.log(data)
        })

I want to set a variable with all the results of the query after the iteration is complete, I have tried using async.each but the queries return undefined objects. Also all the other properties of the content I get aren't passed to the query item properly.

Here's what I have tried so far:

    async.each(checkedList ,function(item, next) {
       let results =[]
 
        query[item.action](req, res, resp, function(err, data) {
            console.log(data)
            results.push(data)
            console.log(results)
            next()
        }.bind({results:results}))
    }, function(err) {
        if (err) console.log(err)
        console.log('done')
    })

The output of console.log(data) is undefined however results does return a value but it belongs to the last item of the array. What am I doing wrong in this scenario?

4
  • Might as well use a regular loop since you want a result after all of those async executions. Otherwise, create a bunch of Promises and use Promise.all(). Commented Feb 6, 2020 at 1:47
  • so i would have to make the query a promise and then i should map the results to the array? Commented Feb 6, 2020 at 1:56
  • Oh, I see you want to do querys. Keeping the async loop makes sense. Wrap the query in a Promise that is resolved after your results.push(data). Then pass all those Promises into Promise.all(). Commented Feb 6, 2020 at 2:12
  • Could you answer how would that look? It would be very helpful Commented Feb 6, 2020 at 2:31

1 Answer 1

1

After reviewing your code I think, but am not sure, you want to do something like:

function whatever(){
  const listData = [];
  return new Promise((resolve, reject)=>{
    async.each(checkedList, (item, next)=>{
      query[item.action](req, res, resp, (err, data)=>{
        listData.push(data);
        if(listData.length === checkedList.length)resolve(listData);
        next();
      });
    }, err=>{
      console.log(err); reject(err);
    });
  });
}
whatever().then(listRes=>{
  console.log(listRes);
});

Personally, I would just call a function when you have all your results instead of returning something:

function whatever(doneFunc){
  const listData = [];
  async.each(checkedList, (item, next)=>{
    query[item.action](req, res, resp, (err, data)=>{
      listData.push(data);
      if(listData.length === checkedList.length)doneFunc(listData);
      next();
    });
  }, err=>{
     console.log(err);
  });
}
whatever(listRes=>{
  console.log(listRes);
});

Sign up to request clarification or add additional context in comments.

1 Comment

when i have an array of two or more items the function returns an error saying ` Error: Callback was already called.`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.