1

I'm new to nodejs and mongoose. I have a database dated from 2009 until now and would like to count number of data of each month, and return as json array. Slow async callback results all dates as Aug. 1, 2014

What is the proper way to implement this?

var dbURL = 'mongodb://localhost/database';
var db = require('mongoose').connect(dbURL);
var c_db = require('./models/models.js').c_db;

var start_date = new Date(2009,0,1);
end_date = new Date(2014,8,1),
next_date = new Date();

var test_json=[];

var total_months = (end_date.getFullYear() - start_date.getFullYear())*12 + (end_date.getMonth() - start_date.getMonth());

var next_date = start_date;

for(var i=0;i<total_months;i++){

  var firstDay = new Date(next_date.getFullYear(), next_date.getMonth(), 1);
  var lastDay = new Date(next_date.getFullYear(), next_date.getMonth() + 1, 0);
  next_date.setDate(lastDay.getDate()+1);

  c_db.count({'shipdate':{'$gte':new Date(firstDay),'$lte':new Date(lastDay)}},function(err,query){

    var item = {
      "Date": firstDay,
      "Count": query
    }
    test_json.push(item);
  });

}

setTimeout(function(){
  console.log(test_json);
},5000);
3
  • c_db.count({'shipdate':{'$gte':new Date(firstDay),'$lte':new Date(lastDay)}},function(err,query){ why are you doing new Date(firstDay)? Are those not already date objects? Commented Sep 2, 2014 at 17:54
  • firstDay and lastDay were hardcoded at the beginning, thx for mentioning :) Commented Sep 2, 2014 at 18:11
  • I guess you could define a callback function, whenFinished, that's called when test_json.length === i - 1. Because of closures, if you put this after test_json.push(item), it'll call it, even if the for loop has finished looping. So something like if (condition) whenFinished() on the line after test_json.push should work. Commented Sep 2, 2014 at 22:42

1 Answer 1

1

Be careful when you are writing javascript with async callbacks. What you want to do is to continue to the next item in the loop when the current async is finished. You can use a the "async" module: https://github.com/caolan/async

var async = require("async");
var dbURL = 'mongodb://localhost/database';
var db = require('mongoose').connect(dbURL);
var c_db = require('./models/models.js').c_db;

var start_date = new Date(2009,0,1);
end_date = new Date(2014,8,1),
next_date = new Date();

var test_json=[];

var total_months = (end_date.getFullYear() - start_date.getFullYear())*12 + (end_date.getMonth() - start_date.getMonth());

var next_date = start_date;

async.timesSeries(total_months, function(n, next) {
  var firstDay = new Date(next_date.getFullYear(), next_date.getMonth(), 1);
  var lastDay = new Date(next_date.getFullYear(), next_date.getMonth() + 1, 0);
  next_date.setDate(lastDay.getDate()+1);

  c_db.count({'shipdate':{'$gte':new Date(firstDay),'$lte':new Date(lastDay)}},function(err,query){

    var item = {
      "Date": firstDay,
      "Count": query
    }
    test_json.push(item);
    next();
  });
}, function(e) {
  console.log(test_json);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.