This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 6 years ago.
I have the below code where I have declared the array var filteredArray = [];. I make use filteredArray to push data in the callback function of a Rest call(This happens inside a for loop).
Once the for loop ends. I want to send the filteredArray to my angular controller. Here is my code. When I put console.log(filteredArray) outside the for loop, I get an empty array. Can someone please help?
assignedToMe: function(req, res) {
CustomerUser.find({
userEmail: {
$in: req.body.userEmail
}
}, function(err, docs) {
var filteredArray = [];
for (var i = 0; i < docs.length; i++) {
var url = config.pythiaApi + config.pythiaApiPrefix + '/quotes/' + docs[i]['quoteId'];
if (req.originalUrl.lastIndexOf('?') !== -1) {
url += req.originalUrl.substr(req.originalUrl.lastIndexOf('?'));
}
restClient.get(url, function(body, response) {
filteredArray.push(body.data)
})
}
console.log(filteredArray) // Gives empty array
res.status(200).json({});
});
}
Use asyncronous loop for async calls. For example async.times
assignedToMe: function(req, res) {
CustomerUser.find({
userEmail: {$in: req.body.userEmail}
}, function(err, docs) {
var filteredArray = [];
async.times(docs.length, function(i, next){
if(err) return next(err);
var url = config.pythiaApi + config.pythiaApiPrefix + '/quotes/'+docs[i]['quoteId'];
if (req.originalUrl.lastIndexOf('?') !== -1) {
url += req.originalUrl.substr(req.originalUrl.lastIndexOf('?'));
}
restClient.get(url, function (body, response) {
next(null, body.data)
})
}, function(err, result){
// result contains your array
res.status(200).json({});
})
});
}
Related
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 6 years ago.
I am trying to retrieve the data from multiple collections using mongoose schema and then send the respond back in JSON format. This is the code I have written.
function GetConnections_V2(req,res){
Connection.find({"user_id":"55006c36c30f0edc5400022d"})
.exec(function(err,connections){
var list = [];
var obj = new Object();
connections.forEach(function(connection){
obj.friend_id = connection.user_id_friend;
User.findById(connection.user_id_friend)
.exec(function(err,user){
if(user !== null) {
obj.friend_email = user.email;
obj.friend_details_id = user.details;
UserDetail.findById(user.details).exec(function (err, details) {
obj.firstname = details.firstname;
obj.lastname = details.lastname;
obj.image = details.image;
list.push(obj);
});
}
});
});
});
console.log(list);
res.send(list);
};
Now on executing this code. It is returning empty array. How do I resolve this problem ?
You are calling async functions inside for loop. That's why list is empty.
You can use async to solve this problem.
var async = require('async');
function GetConnections_V2(req,res){
Connection.find({"user_id":"55006c36c30f0edc5400022d"})
.exec(function(err,connections){
var list = [];
async.each(connections, function(connection, callback){
var obj = new Object();
obj.friend_id = connection.user_id_friend;
User.findById(connection.user_id_friend)
.exec(function(err,user){
if(user !== null) {
obj.friend_email = user.email;
obj.friend_details_id = user.details;
UserDetail.findById(user.details).exec(function (err, details) {
obj.firstname = details.firstname;
obj.lastname = details.lastname;
obj.image = details.image;
list.push(obj);
callback();
});
} else {
callback();
}
});
}, function(err){
return res.send(list);
});
};
}
Hope it helps you.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
Problem resolved on Asynchronously solution to check data from database kinds of loop clause
with this below code i can generate simple array as jsonArray into checkUserMobileNumberAsEwallet function, but i can't pass it out of that to send inside client,
with socket.emit('syncContacts', accountNumbers) i get [] result on accountNumbers, but into if (success) { statement array successful created and pushed into accountNumbers array
socket.on('syncContacts', function (data) {
var accountNumbers = [];
for (var i = 0; i < data.length; i++) {
checkUserMobileNumberAsEwallet(data[i].mobileNumber, function (success) {
if (success) {
accountNumbers.push({ewalletNumber: this.mobileNumber});
console.log(accountNumbers);
}
}.bind({mobileNumber: data[i].mobileNumber}));
}
console.log(accountNumbers);
socket.emit('syncContacts', accountNumbers);
});
function checkUserMobileNumberAsEwallet(mobileNumber, callback) {
var mobileNumber = mobileNumber.substr(1, mobileNumber.length);
var query = "SELECT id FROM userEwallets WHERE ewalletNumber LIKE '%" + mobileNumber + "'";
connection.query(query, function (err, results) {
if (err) return callback(false);
if (results.length === 0)
return callback(false);
else {
return callback(true);
}
});
}
Updated after post comments:
socket.on('syncContacts', function (data) {
//console.log(accountNumbers);
//socket.emit('syncContacts', accountNumbers);
async.parallel(
[
function (callback) {
var accountNumbers = [];
for (var i = 0; i < data.length; i++) {
checkUserMobileNumberAsEwallet(data[i].mobileNumber, function (success) {
if (success) {
accountNumbers.push({ewalletNumber: this.mobileNumber});
console.log(accountNumbers);
}
}.bind({mobileNumber: data[i].mobileNumber}));
}
callback(success, accountNumbers);
}
],
function (success, results) {
console.log("results " + results.toString());
socket.emit('syncContacts', results);
});
});
function checkUserMobileNumberAsEwallet(mobileNumber, callback) {
var mobileNumber = mobileNumber.substr(1, mobileNumber.length);
var query = "SELECT id FROM userEwallets WHERE ewalletNumber LIKE '%" + mobileNumber + "'";
connection.query(query, function (err, results) {
if (err) return callback(false);
if (results.length === 0)
return callback(false);
else {
return callback(true);
}
});
}
Based on our code, it seems like checkUserMobileNumberAsEwallet is an asynchronous event, which accepts a function callback. That would mean that the for-loop would execute, which would queue up executions to checkUserMobileNumberAsEwallet. Immediately after the for-loop executes, console.log would correctly output the empty array accountNumbers and emit the event through the socket. Then the callback functions to each checkUserMobileNumberAsEwallet execution would begin to execute, and log the accountNumbers array which now has data.
This can be solved in a few ways, but likely the easiest and most readable would be to create Promises, and act on the Promises when they resolve. Personally I like the 'when' promise library, but many libraries could help to solve this problem. https://github.com/cujojs/when/blob/master/docs/api.md#whensettle
The problem is that you have no control over when the code inside the callback executes, and it ends up executing after you've already called socket.emit.
You can either a) use an async aggregator to call a callback for each successful mobile number and then, when all of those have finished, call the socket.emit call OR b) do something like what I did below. Alter your checkUserMobileNumberAsEwallet function to accept and verify an array of mobile numbers, and pass the successful numbers to your callback function
Try the following:
socket.on('syncContacts', function (data) {
var accountNumbers = [];
var mobileNumbers = [];
for (var i = 0; i < data.length; i++) {
mobileNumbers.push(data[i].mobileNumber);
}
checkUserMobileNumberAsEwallet(mobileNumbers, function (successfulNumbers) {
if (successfulNumbers.length > 0) {
for (var i = 0; i < successfulNumbers.length; i++) {
accountNumbers.push({ewalletNumber: successfulNumbers[i]});
}
socket.emit('syncContacts', accountNumbers);
}
}.bind({mobileNumbers: mobileNumbers}));
});
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 6 years ago.
When I use a simple for loop to access array values, the index variable gets lost therefore unable to access array. Using index number instead of variable works but not variable. This is the most annoying code ever.
/* jshint esnext: true, asi: true */
var neo4j = require('node-neo4j')
// Create the neo4J object
var db = new neo4j(serverURI)
exports.addPerson = (body, callback) => {
if (body.skills) {
var sentSkills = body.skills
var arraySkills = sentSkills.split(',')
}else {
var sentSkills = []
}
const sentName = body.name
const sentEmail = body.email
const sentUsername = body.username
const sentPassword = body.password
const lecturerStatus = body.lecturer
db.readNodesWithLabelsAndProperties('Person',{ email: sentEmail }, function (err, node) {
if (err) {
return console.log(err)
}
if (node.length > 0){
// The user already exists
callback({code:401,status:'failed',message:'Person already exsits with the name '+sentName,data:sentName})
}
else {
// Insert new Person
db.insertNode({
name: sentName,
email: sentEmail,
skills: sentSkills,
username: sentUsername,
password: sentPassword,
lecturer: lecturerStatus
}, 'Person', function (err, node) {
personNode = node
if (err) {
return console.log(err+1)
}
else {
// I hate you for not working
// The i = 0 variable is not accessible -> arraySkill[i]^.trim()
// ERROR: cannot read property trim of undefined
console.log("success")
for (i = 0; i < arraySkills.length; i++){
arraySkills = body.skills.split(',')
db.cypherQuery("MATCH (s:Skill {name:'"+arraySkills[i].trim()+"'}) RETURN s", function(err, node){
if (err){
console.log("ERROR1")
console.log(err)
}
else {
console.log(arraySkills[0])
if (node.data == '')
{
db.cypherQuery("CREATE (s:Skill {name:'"+arraySkills[i].trim()+"'}) RETURN s", function(err, node){
if (err){
console.log("ERROR2")
console.log(err)
}
else {
console.log(node)
db.cypherQuery("MATCH (p:Person), (s:Skill) WHERE p.name = '"+sentName.trim()+"' AND s.name = '"+arraySkills[i].trim()+"' CREATE (p)-[r:knows]->(s) RETURN r", function(err, node){
if (err){
console.log("ERROR3")
console.log(err)
}
else {
console.log(node)
console.log("Success")
}
})
}
})
}
}
})
};
}
})
// Output node data.
callback({code:201,status:'success',message:'Person Added In '+sentName+' found...',data:node})
}
})
}
That's a closure problem, to fix it you have to move your $http call to a new function like this.
for (i = 0; i < arraySkills.length; i++){
var skills = body.skills.split(',');
dbQuery(skills,i); // In this function you have to write all the stuff you got under arraySkills = body.skills.split(',')
}
var veh = [];
app.get('/updateProf', isLoggedIn, function(req, res) {
for (var i = 0; i < req.user.local.vehicles.length; i++){
Vehicles.findById(req.user.local.vehicles[i], function(err, vehicle) {
veh.push(vehicle);
console.log("GET Json: " + veh);
});
}
console.log(veh);
res.json(veh);
veh.length = 0;
});
So I am doing a get request to obtain all my vehicles that a user owns and return its json, it works fine after a refresh, but when I go to the page it shows a empty array on the initial load, if I refresh the page, the array is populated. I think the issue is something to do with it being asynchronous but I'm having a hard time thinking this way and need some advice on how to tackle this.
Yes!!
You will have to wait for all of the callbacks to finish before returning the JSON.
A solution is to keep a count of how many callbacks have been executed and when all of them have been executed you can return the JSON.
var veh = [];
app.get('/updateProf', isLoggedIn, function(req, res) {
number_processed = 0;
total = req.user.local.vehicles.length;
for (var i = 0; i < req.user.local.vehicles.length; i++){
Vehicles.findById(req.user.local.vehicles[i], function(err, vehicle) {
if(!err){
veh.push(vehicle);
}
number_processed = number_processed + 1;
if(number_processed === total){
res.json(veh);
}
console.log("GET JSON: " + veh);
});
}
veh.length = 0;
});
If you are using a more recent version of Mongoose, then you can directly use Promises that are returned by Mongoose for each query.
For example, your query can be simplified to
Vehicles.find({ _id: {'$in': req.user.local.vehicles }})
.exec()
.then(function(vehicleArr) {
res.json(vehicleArr);
});
Note that I use the $in operator to directly translate your loop into an IN condition, which takes an array of what you want to compare (in this case, it's an array of IDs)
The then() function just executes on completion of the query.
Async is a utility library to deal with this.
var async = require('async');
app.get('/updateProf', isLoggedIn, function(req, res) {
async.map(req.user.local.vehicles, function(vehicle, cb){
Vehicles.findById(vehicle, function(err, vehicle) {
if (err) cb(err, null);
console.log('GET Json: ' + vehicle);
cb(null, vehicle);
});
}, function (err, results) {
console.log(results);
res.json(results);
});
});
I am trying to push some values to array by fetching data from Jenkins APIs, like below.
buildNum = 14;
async.waterfall([
function(callback){
for ( var i = buildNum; i > (buildNum-5); i--) {
(function(){
jenkins.build_info('BuildDefinitionRequest', i, function(err, data) {
if (err){ return console.log(err); }
var tmpObj = {};
tmpObj.jobID = data.fullDisplayName;
tmpObj.result = data.result;
tmpObj.dateTime = data.id;
console.log(tmpObj);
finalArray.push(tmpObj);
});
})();
}
callback(null, finalArray, 1);
},
function(finalArray, value, callback){
console.log(finalArray, value);
callback(null, 'done');
}
],function(err, result){
});
But "callback(null, finalArray, 1);" is getting called before the for loop finish its execution.
When I am printing the value of "finalArray" inside the for loop I am able to see all the values.
Technically the for loop has finished executing, but the jenkins.build_info calls haven't. You cannot make async calls inside of a for loop like that and expect the for loop to only finish after all the calls are complete. You're already using async, so this is an easy fix. I would do something like this:
var buildNum = 14;
var builds = [];
// just builds a collection for async to operate on
for(var i = buildNum; i > (buildNum - 5); i--) {
builds.push(i);
}
var finalArray = [];
async.each(builds, function(build, next) {
jenkins.build_info('BuildDefinitionRequest', build, function(err, data) {
if (err) { next(err); }
var job = {
jobID: data.fullDisplayName,
result: data.result,
dateTime: data.id
};
finalArray.push(job);
next();
});
}, function(err) {
// this won't be called until all the jenkins.build_info functional have completed, or there is an error.
console.log(finalArray);
});