Asynchronous Iteration of Array of JSON - node.js

I'm using Node.js with MongoDB, to be more specific, MongoLab and Mongoose.
In the DB, I have two collections, pours and users. A user object would be linked to multiple pour objects with a shared cid.
I need to iterate through an array of user objects. But for loop somehow doesn't work with asyn functions that I would like to use to modify my array.
express.leaderboard = new Array();
mongoose.Users.find(function(err, users) {
for (var i = 0; i < users.length; i++) {
express.leaderboard[express.leaderboard.length] = users[i];
};
for (i = 0; i < express.leaderboard.length; i++){
updateOunces(i, function(a, fluidOunces){
console.log(a);
express.leaderboard[a].set('totalOunces', fluidOunces);
});
}
});
And this is my function that would retrieve the total fluidOunces for a user.
function updateOunces(i, callback){
//console.log(express.leaderboard[b].cid);
mongoose.Pours.find({
"cid": express.leaderboard[i].cid
}).exec(function(err, result) {
var userOunces = 0.0;
if (!err) {
for (i = 0; i < result.length; i += 1) {
for(j = 0; j < result[i].pour.length; j += 1){
userOunces += result[i].pour[j].fluidOunces;
}
}
callback(i, userOunces);
return;
express.leaderboard[i].set ('totalOunces' , userOunces);
} else {
console.log(err)
};
});
};
Is there a way to iterate and add a new property to each object in the leaderboard array? Using ASYN? Thank you!

use async library.
Example)
mongoose.Users.find(function(err, users) {
async.each(users, function(user, callback) {
// perform updates here, per user.
callback();
}, function(err) {
// everything is complete.
});
});

Related

node js: how to let a function execute after a bunch of database reads?

I'm trying to read multiple data from database, put them into an array, and deal with the array. The code looks like this:
var array = [];
// first for loop
for (var i = 0; i < 10; i++) {
db.read(i, function(rs) { // read data from database and put it into array
array.push(rs);
}
}
// second for loop
for (int i = 0; i < 10; i++) {
console.log(array[i]);
}
However, this piece of code will not work because the second for loop will execute before the first loop ends. Is there any good solutions? BTW, I've used promise like this:
var array = [];
var promise = new Promise(function(resolve, reject) {
for (var i = 0; i < 10; i++) {
db.read(i, function(rs) { // read data from database and put it into array
array.push(rs);
}
}
resolve(array);
};
promise.then(function(array) {
for (int i = 0; i < 10; i++) {
console.log(array[i]);
}
};
It doesn't work either, it seems that the resolve will not wait until all the db read operations finish. So when will the resolve wait until all the previous code finish?
the best way to complete your requirement use async.
var name = ["shekhar", 'vishal', "param", "abhishek", 'amit'];
for (var i = 0; i < 5; i++) {
var item = name[i];//execute your first loop here
}
async.eachSeries(name, processData, function (err) {
if (err) {
res.json({status: 0, msg: "OOPS! How is this possible?"});
}
res.json("Series Processing Done");
})
function processData(item, callback) {
//execute your second loop here
});
Using promises, the asynchronous block of code should return a promise.
The database read function should return a promise, and can easily be rewritten as
function readFromDatabase(i){
return new Promise(function(resolve, reject){
db.read(i, function(rs) { // read data from database and put it into array
resolve(rs);
}
});
}
You then run your loop and get all the values from the database, await the promises and access the result in the .then() function where you can iterate of them.
//We create an empty array to store our promises
var arrayOfPromises = [];
for (var i = 0; i < 10; i++) {
//We push each promise to the array
arrayOfPromises.push(readFromDatabase(i));
}
//We wait until all promises have
Promise.all(arrayOfPromises)
.then(function(arrayOfResults){
//arrayOfResults is an array with the resolved values from the 'readFromDatabase' function
})
.catch(function(){
//Handle errors
});

Mongoose two request depending each other

I have images model and users model.
every image has a user_id field of a user and I want to get the picture of the user and name, add it to the image object and return an array of images.
When I am trying to add author_image field to ONE image I don't have any errors,
But when I am looping over all the images the app crashes the output is that imagesData is undefined as well as userData.
I tried using promises but again I get an error.
What is the best way I can do that without the undefined error?
router.route('/images/all')
.get(function(req,res){
var response = {};
var imagesData = {};
images.find({}).lean().exec(function(err,data){
// console.log(data);
imagesData = data;
if (!err) {
for (var i = 0; i < imagesData.length; i++) {
users.find(({'_id': imagesData[i].user_id}),function(err,userData){
console.log(userData);
imagesData[i].author_pic = userData[0].profile_image;
});
}
}
res.json(imagesData);
});
});
What you missed out is that find operation is not a synchronous operation. So all your find operation immediately move on to the next line.
Although there are multiple ways to handle such situation, I tend to use promises (Q library).
The code would look like this
var Q = require('q');
var defer = Q.defer();
images.find({}).lean().exec(function (err, data) {
// console.log(data);
imagesData = data;
var promiseArr = [];
if (!err) {
for (var i = 0; i < imagesData.length; i++) {
var innerDefer = Q.defer();
users.find(({'_id': imagesData[i].user_id}), function (err, userData) {
console.log(userData);
defer.resolve(userData[0].profile_image);
});
promiseArr.push(innerDefer);
}
}
Q.all(promiseArr).then(function (results) {
for (var i = 0; i < imagesData.length; i++) {
if (Q.isPromise(results[i])) {
results[i] = results[i].valueOf();
}
imagesData[i].author_pic = results[i];
}
res.json(imagesData);
})
});
In this case I am using the Q.all method which basically waits for all the find to finish, and executes only then.

i want want to perform looped serial database insertion in mongodb on node js by bluebird promises,but unable to do so

using for loop i want to insert data by mongoose promises and handle output one by one
for (i = 0; i < array.length; i++) {
if (xyz) {
return Service.registerInterestsAsync(registerTruckArray)
.then(function(x) {
passoutputinArray(x)
});
}
}
I haven't used bluebird but you can do this by writing your own recursive function.
var handleResponse = function(err, data) {
//perform action
}
function inserIntoMongo(dataArray, counter) {
if( counter < dataArray.length) {
mongoose.InsertDocument(dataArray[counter], function(err, data) {
handleResponse(err, data);
counter = counter + 1;
insertIntoMongo(dataArray, counter);
}
}
}

MongoDB Collation loop

I am trying to run a find inside another find and I am not getting any results from the second find operation.
User.find({}, function (err, docs) {
for (i = 0; i < docs.length; i++) {
var tmp = '';
UserGroups.find({userName: docs[i].userName}, function (errin, groups) {
for (g = 0; g < groups.length; g++) {
tmp += ", " + groups[g].groupName;
//console.log(groups[g].groupName);
}
});
console.log(tmp);
//docs[i].group = that;
docs[i].username = decrypt(docs[i].username);
docs[i].password = '';
}
res.render('users', {users: docs});
});
Your UserGroups.find is going to be run asynchronously therefore console.log(tmp) is going to be run before your UserGroups.find has a chance to finish and your call is going to return before you get any results. If you want the results of the UserGroup.find you need to move all of your logic into that callback.
EDIT
This is I believe a far better approach in terms of predictability and query performance. Your previous approach the UserGroup.find being called n number of times. N being the number of users in your database. This approach the database is only queried twice. Once to get all the users and second to get all the groups.
User.find({}, function (err, docs) {
//Get all the usernames before executing the UserGroups query
var userNames = [];
users.forEach(function(element) {
userNames.push(element.userName);
});
UserGroups.find({userName: {$in : userNames}}, function (errin, groups) {
for (var i = 0; i < docs.length; i++) {
//get all the groups that belong to this user
var userGroups = groups.filter(function(value) {
return value.userName === docs[i].userName;
});
var tmp = "";
userGroups.forEach(function(element){
tmp += "," + element.groupName
});
//docs[i].group = that;
docs[i].username = decrypt(user[i].username);
docs[i].password = '';
}
res.render('users', {users: docs});
});
});
Also since it appears you are using Mongoose you can use the built in populate feature in Mongoose to "join" collections together

Synchronous for loop in node js

So let's say I have the following for loop
for(var i = 0; i < array.length; i++){
Model.findOne({ _id = array[i].id}, function(err, found){
//Some stuff
});
}
How do I make this code work? Every time I run it I get array[i] = undefinedbecause the mongo-db query is asynchronous and the loop has already iterated 5 times by the time the first query is even completed. How do I go about tackling this issue and waiting for the query to complete before going on to the next iteration?
This doesn't specifically answer your question, but addresses your problem.
I'd use an $in query and do the filtering all at once. 20 calls to the db is pretty slow compared to 1:
// grab your ids
var arrayIds = myArray.map(function(item) {
return item._id;
});
// find all of them
Model.find({_id: {$in: arrayIds}}, function(error, foundItems) {
if (error) {
// error handle
}
// set up a map of the found ids
var foundItemsMap = {};
foundItems.forEach(function(item) {
foundItemsMap[item._id] = true;
});
// pull out your items that haven't been created yet
var newItems = [];
for (var i = 0; i < myArray.length; i++) {
var arrayItem = myArray[i];
if ( foundItemsMap[arrayItem._id] ) {
// this array item exists in the map of foundIds
// so the item already exists in the database
}
else {
// it doesn't exist, push it into the new array
newItems.push(arrayItem);
}
}
// now you have `newItems`, an array of objects that aren't in the database
});
One of the easiest ways to accomplish something like you want is using promises. You could use the library q to do this:
var Q = require('q');
function fetchOne(id) {
var deferred = Q.defer();
Model.findOne({ _id = id}, function(err, found){
if(err) deferred.reject(err);
else deferred.resolve(found);
});
return deferred.promise;
}
function fetch(ids, action) {
if(ids.length === 0) return;
var id = ids.pop();
fetchOne(id).then(function(model) {
action(model);
fetch(ids, action);
});
}
fetch([1,2,3,4,5], function(model) { /* do something */ });
It is not the most beautiful implementation, but I'm sure you get the picture :)
Not sure if this is the right way, it could be a bit expensive but this how i did it.
I think the trick is to pull all your data and then looking for an id match.
Model.find(function(err, data) {
if (err) //handle it
for (var i=0; i<array.length; i++) {
for (var j=0; ij<data.length; j++) {
if(data[j].id == array[i].id) {
// do something
}
}
}
}

Resources