I use acync.seriesnode.js. in my program. I am trying to asynchronously scroll through a collection of mongooses with async.each. Here is the code:
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 correct way to loop asynchronously in a mongoose collection
source
share