I am trying to loop through an array and find the amount of tickets assigned to each person.
Unfortunately, I noticed that my taskcount is getting the same values but in different order, because of its asynchronous nature.
Some queries might take long and so the ones that gets finished first gets inserted and hence my array has the same values but in different order. Now, I want to avoid that and make it so, that once a query gets completed, only then the next value from the array is being picked up and pushed to search from the db. How can i modify my existing code.
exports.find_task_count = function(callback) {
var names = ['Evan', 'Surajit', 'Isis', 'Millie', 'Sharon', 'Phoebe', 'Angel', 'Serah']
var taskcount = []
var resultsCount = 0;
for (var i = 0; i < names.length; i++) {
_tasks.find({'assignee': names[i]}, function (err, tickets) {
resultsCount++
if (err) {
console.log(err)
return callback(err)
} else {
taskcount.push(tickets.length)
if (resultsCount === names.length) {
return callback(taskcount);
taskcount=[]
}
}
})
}
}
You can use the async module designed to handle such scenarios.
I have updated the code as follows
var async = require('async');
exports.find_task_count = function (callback) {
var names = ['Evan', 'Surajit', 'Isis', 'Millie', 'Sharon', 'Phoebe', 'Angel', 'Serah'];
async.map(names, function (name, iterateeCallback) {
_tasks.find({ 'assignee': name }, function (err, tickets) {
if (err) {
return iterateeCallback(err);
}
return iterateeCallback(null, tickets.length);
});
}, function (error, results) {
if (error) {
return callback(error);
}
return callback(null, results);
});
}
As per the documentation of async
Note, that since this function applies the iteratee to each item in parallel, there is no guarantee that the iteratee functions will complete in order. However, the results array will be in the same order as the original coll.
if you still want to process the array in series use mapSeries instead of map in the above code
Related
I have an array of Courses [course1, course2, ..., courseN]. Each course has a Hero UUID, which matches the UUID of an object in another collection.
// This is actually another db query but imagine it's an array
var courses = [{
"courseName": "Sample course name 1",
"hero": "a3f6f088-7b04-45e8-8d3b-d50c2d5b3a2d"
}, {
"courseName": "Sample course name 2",
"hero": "1b46227a-c496-43d2-be8e-1b0fa07cc94e"
}, {
"courseName": "Sample course name 3",
"hero": "c3bae6bf-2553-473a-9f30-f5c58c4fd608"
}];
I need to iterate over all courses, get the hero uuid and do a query to the Heroes collection then when the query is complete add the hero information to the course object.
The problem is that all queries are fired so rapidly that MongoDB returns them in arbitrary order. It receives all 3 hero uuids in order but it will sometimes return the third one before the first one, etc. Is there a way for one query to complete then do the other one, etc.?
What I am doing right now is:
var newCourses = courses;
var counter = 0;
courses.forEach(function (course) {
var courseHeroUuid = course.hero;
// This function does the query by uuid and returns the doc
getHeroByUuid(courseHeroUuid, function (err, result) {
if (err) {
next(err);
}
// Replace the hero UUID with the hero document itself
newCourses[counter].hero = result[0];
if (++counter == courses.length) {
next(null, newCourses);
}
}
});
This is a function inside an async.waterfall array, this is why I track the counter and call next() to go on. I know I can use async.each for the iteration, I tried it didn't help out.
This is the query I am doing.
function getHeroByUuid(heroUuid, callback) {
Hero.find({uuid: heroUuid}, function (err, result) {
if (err) {
callback(err);
}
callback(null, result);
})
}
This happens:
http://i.imgur.com/mEoQfgH.png
Sorry to answer my own question but I figured it out. What I needed was right under my nose.
I ended up using the async.whilst() function, documentation is right here and does exactly what I need - execute the next iteration of the loop after the result from the previous one is returned.
My code now looks like this:
var newCourses = courses;
var courseItemsLength = courses.length;
var counter = 0;
async.whilst(
function () {
return counter < courseItemsLength;
}, function (callback) {
var heroUuid = allCourses[counter].hero;
getHeroByUuid(heroUuid, function (err, result) {
if (err) {
next(err);
}
newCourses[counter].hero = result.name;
counter++;
if (err) {
callback(err);
}
callback();
});
}, function (err) {
if (err) {
next(err);
}
next(null, newCourses);
});
I have a MongoDB databse set up with some objects that have a unique code (not the primary key).
I should also note that I'm using NodeJS and this code is in my server.js to connect to the MongoDB database.
To generate a new code, I generate one randomly and I want to check if it already exists. If not then we use it no problem, but if it already exists I want to generate another code and check it again. This is the code I use to check if the id already exists:
function createPartyId(callback) {
var min = 10000, max = 99999;
var partyId = -1, count = -1;
async.whilst(
function () { return count != 0; },
function (callback) {
partyId = min + Math.floor(Math.random() * (max - min + 1));
partyId = 88888;
getPartyIdCount(partyId, function(num) {
count = num;
});
},
function (err) {
}
);
}
function getPartyIdCount(partyId, callback) {
count = -1;
db.db_name.find({id: partyId}, function(err, records) {
if(err) {
console.log("There was an error executing the database query.");
callback(count);
}
count = records.length;
callback(count);
});
}
First of all, is there any particular reason you're not using a simple number increment sequence? This type of code is prone to inefficiency, the more numbers you generate the more chance you have of collisions which means you're going to be spending more time on generating an ID for your data than you are on the rest of your processing. Not a good idea.
But I can still tell you what's going wrong.
OK, so getPartyIdCount() will only, ever, always, without fail, return undefined (or, basically, nothing).
Your mongo call processes the return value in a callback, and that callback doesn't assign its value to anything, so return records.length just gets lost into nothingness.
You've mixed up createPartyId(), which it appears you want to run synchronously, with your mongo call, which must run asynchronously.
return always goes with the nearest containing function, so in this case it goes with function(err, records), not function getPartyIdCount(partyId).
(Expanding my comment from above)
The issue is that createPartyId is an asynchronous function, but you're trying to return the value synchronously. That won't work. Once you touch an async operation, the rest of the call stack has to be async as well.
You don't include the code that's calling this, but I assume you want it to be something like:
var partyId = createPartyId();
// do stuff...
That's not going to work. Try this:
function createPartyId(callback) {
var min = 10000, max = 99999;
var partyId = -1, count = -1;
async.whilst(
function () { return (count == 0); },
function (callback) {
partyId = min + Math.floor(Math.random() * (max - min + 1));
partyId = 88888;
getPartyIdCount(partyId, function(err, num) {
if (!err) {
count = num;
}
callback(err);
});
},
function (err) {
// this is called when the loop ends, error or not
// Invoke outer callback to return the result
callback(err, count);
}
);
}
function getPartyIdCount(partyId, callback) {
count = -1;
db.db_name.find({id: partyId}, function(err, records) {
if(err) {
console.log("There was an error executing the database query.");
callback(err);
}
count = records.length;
callback(null, count);
});
}
(I've also adopted the default node.js convention of always returning errors as the first argument to callback functions.)
So, to use this you would do:
getPartyId(function (err, num) {
if (err) { return aughItFellOver(err); }
// do stuff
});
I'm new to Node.js. I'm trying to use the Node Redis plugin. What I'm trying to do is get the list of key/value pairs from a REDIS database. The asynchronous nature of Node is creating a challenge for me. When I try to print the key/value pairs, my array is empty. I think its because my query to the database hasn't completed yet.
The reason I'm creating an array first instead of just printing out the key/value pairs, is because I want to sort the results alphabetically by either key or value. Currently, I"m trying the following:
redisClient.on('connect', function() {
var pairs = [];
redisClient.keys('*', function (err, keys) {
if (err) { return console.log(err); }
for(var i = 0, len = keys.length; i < len; i++) {
(function(key) {
redisClient.get(key, function(err, v) {
pairs.push({ key: key, value: v });
});
})(keys[i]);
}
});
// List of key/value pairs ordered alphabetically
console.log(pairs);
});
Can someone tell me how to get beyond this issue? Thank you
You are right in your assertion that when console.log is executed, nothing has happened yet. To deal with this kind of problem in Node.js, some people like to use modules like async and others prefer to use promises (a popular library for promises is Q).
Here is a solution using async.
var async = require('async');
redisClient.on('connect', function() {
redisClient.keys('*', function (err, keys) {
if (err) { return console.log(err); }
async.map(keys, function(key, callback) {
redisClient.get(key, function(err, value) {
callback(null, {key: key, value: value});
});
}, function(err, pairs) {
console.log(pairs);
});
});
});
async just happens to have a map function that takes an array, applies a function asynchronously to each elements and creates an array with the results.
You need to use recursion:
do = function(i, data, callback){
// if the end is reached, call the callback
if(data.length === i+1)
return callback()
obj = data[i]
redisClient.get(key, function(err, v) {
pairs.push({ key: key, value: v });
i++;
// call do with the next object
do(i, data, callback)
});
}
// Redis...
do(0, keys, function(){
// List of key/value pairs ordered alphabetically
console.log(pairs);
});
Maybe the results ain't weird, but I started using Node 1-2 months ago so for me they are...
I have a loop which sorts out every other value of the array returned by hgetall (Redis command) and in that loop I call a function to get all values from another table with keys stored in the sorted array. This was more difficult to explain than I thought. Here's my code:
Pastebin: http://pastebin.com/tAVhSUV1 (or see below)
function getInfo (cn, callback) {
var anArray = [];
redis_client.hgetall('chat_info:' + cn, function (err, vals) {
if(err) { throw err; }
for(i in vals) {
anArray.push(vals[i]);
}
return callback(anArray);
});
}
redis_client.hgetall('chat_rooms:' + POST.chat_name, function (err, val) {
if(err) { throw err; }
var vars = [],
rArr = [];
for (i in val) {
vars.push(i);
}
for(var i = 0; i < vars.length; i += 1) {
if(i%2 === 0) {
getInfo(vars[i], function (hej) {
rArr.push(hej);
});
}
}
});
The callback from the call to getInfo() is executed after the entire loop. Am I missing out on something here? Because it can't do that, right? (when I use rArr (right after the loop) it's empty, nbBut if I log it in the callback it gets logged after everything else written after the loop)
Yes, that's probably normal.
Understand that callbacks are executed after the hgetall call. Which mean that when the redis functions receive somehting it will call the callbacks. In other words, all the callbacks can be executed later.
As javascript only works in one thread, the calls to hgetall should be blocking to be executed as they come in the for loop. But as you're more certainly using async IO. The for loop ends and then it will start calling each callbacks that were queued inside the javascript event loop.
Edit
Unfortunately, to achieve what you're trying to do, you should wrap your code inside many other callbacks. You can use this project to make it easier: https://github.com/caolan/async
You should be able to install it using npm install async.
You'd have to do something like that:
function getInfo (cn) {
return function(callback) {
var anArray = [];
redis_client.hgetall('chat_info:' + cn, function (err, vals) {
if(err) { throw err; }
for(i in vals) {
anArray.push(vals[i]);
}
return callback(anArray);
});
};
}
redis_client.hgetall('chat_rooms:' + POST.chat_name, function (err, val) {
if(err) { throw err; }
var vars = [],
rArr = [],
callbacks = [];
for (i in val) {
vars.push(i);
}
for(var i = 0; i < vars.length; i += 1) {
if(i%2 === 0) {
callbacks.push(getInfo(vars[i]));
}
}
async.series(callbacks, function (err, results) {
// Final code here
});
});
I'm starting to learn node.js, and to aggregate multiple rss feeds in a single one, I'm fectching the feeds and then recreate a unique feed from the data I fecthed.
So, in order to handle multiple http requests asynchronously I use https://github.com/caolan/async#forEach which does the job.
But I can't figure how to return a value (the rss xml feed in my case).
Here is my code :
function aggregate(topic) {
async.forEach(topic.feeds,
function(item, callback) {
parseAndProcessFeed(item, callback);
},
function(err) {
// sort items by date
items.sort(function(a, b) {
return (Date.parse(b.date) - Date.parse(a.name));
});
var rssFeed = createAggregatedFeed();
console.log(rssFeed);
return rssFeed;
}
);
}
with console.log(rssFeed) I can see the rss feed, so I think I'm missing something obvious.
How can I return the rssFeed value?
Some help to get me back on seat would be great, thanks!
Xavier
You can't return value from asyncronious function. You need pass it into callback function. Like this:
function aggregate(topic, callback) {
async.forEach(topic.feeds,
function(item, callback) {
parseAndProcessFeed(item, callback);
},
function(err) {
// sort items by date
items.sort(function(a, b) {
return (Date.parse(b.date) - Date.parse(a.name));
});
var rssFeed = createAggregatedFeed();
callback(err, rssFeed);
}
);
}
aggregate(someTopic, function(err, result) {
// here is result of aggregate
});