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?
Promise.all().querys. Keeping theasyncloop makes sense. Wrap thequeryin a Promise that isresolved after yourresults.push(data). Then pass all those Promises intoPromise.all().