Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronously loop through the mongoose collection with async.each

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

like image 464
Michael Avatar asked Feb 11 '23 07:02

Michael


1 Answers

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`
  });
}
like image 97
djanowski Avatar answered Feb 13 '23 20:02

djanowski