4

I am using acync.series on my node.js program. I am trying to asynchronously loop through the mongoose collection with async.each. Here is code so far:

var async = require('async');
var mongoose = require('mongoose');
var usersData;
async.series([
    function(callback) {
        mongoose.connect("mongodb://localhost/****");
        var db = mongoose.connection;
        db.on('error', console.error.bind(console, 'connection error...'));
        db.once('open', function callback() {
            console.log('db opened!');
        });
        callback();
    },
    function(callback) {
        users = mongoose.model('User', new mongoose.Schema({name: String,age: Number}));

        users.find(function(err, userFound) {
            if (err) {console.log(err);}
            usersData = userFound;
        });
        callback();
    },
    function(callback) {
        async.each(usersData, function(userData, callback) {
            some code....
        }, callback);
    }
])

When I run it i get the following error from async:

    if (!arr.length) {
            ^
TypeError: Cannot read property 'length' of undefined

What is the right way to asynchronously loop through the mongoose collection

Michael
  • 15,386
  • 36
  • 94
  • 143

3 Answers3

7

Because async/await will be ES7 and is already quite popular via transpilation, this is a top result when googling for Mongoose and async.

While not answering the original question, I thought I'd leave this for future reference.

Using native Promises (note all users are processed in parallel):

const User = mongoose.model('User', new mongoose.Schema({
  name: String,
  age:  Number
}));


function processUsers() {
  return mongoose.connect('mongodb://localhost')
    .then(function() {
      return User.find();
    })
    .then(function(users) {
      const promises = users.map(function(user) {
        return // ... some async code using `user`
      });

      return Promise.all(promises);
    });
});


processUsers()
  .then(function() {
    console.log('Finished');
  })
  .catch(function(error) {
    console.error(error.stack);
  });

Using Mongoose's eachAsync every user is processed sequentially:

function processUsers() {
  return mongoose.connect('mongodb://localhost')
    .then(function() {
      return User.find().cursor().eachAsync(function(user) {
        return // ... some async code using `user`
      });
    })
});

Using async/await:

async function processUsers() {
  await mongoose.connect('mongodb://localhost');

  await User.find().cursor().eachAsync(async function(user) {
    await // ... some async code using `user`
  });
}
djanowski
  • 5,610
  • 1
  • 27
  • 17
2

I think in our case better use waterfall, like this

async.waterfall([
    function (callback) {
        mongoose.connect("mongodb://localhost/****");
        var db = mongoose.connection;

        db.on('error', console.error.bind(console, 'connection error...'));
        db.once('open', function () {
            callback(null);
        });
    },
    function(callback){
        var users = mongoose.model('User', new mongoose.Schema({
            name: String,
            age: Number
        }));

        users.find(function(err, userFound) {
            if (err) {
                return callback(err);
            }

            callback(null, userFound);
        });

    },
    function(usersData, callback){
        async.each(usersData, function(userData, callback) {

        }, callback);
    }
], function (err, result) {

});

There is good explanation about difference waterfall vs series

Community
  • 1
  • 1
Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
0

Another option:

functionProcess = (callback) => {

  userModel.find().cursor().eachAsync(user => {
    return user.save().exec();        // Need promise
  }).then(callback);     //Final loop

}
Jesus
  • 752
  • 7
  • 5