Saturday, 6 May 2017

NodeJS: Mongoose return wrong results from MongoDB although Mongo shell return correct ones

I'm using Express to build my first NodeJS app with the MongoDB as the database and Mongoose as it's modeling tool. I'm following a tutorial from MDN as my guidance. The problem is when I query MongoDB from my app it returns wrong results, but when I query it from Mongo shell I get correct results. An example scenario is with the code below which return zero (0) on every count done on my collections though there are documents in there ( which are correctly returned by the Mongo shell):

var async = require('async');

var GeneralUser = require('../models/guser');
var Truck = require('../models/truck');
var Trip = require('../models/trip');
var TruckLocation = require('../models/location');

exports.index = function(req, res, next) {
    async.parallel({
        user_count: function(callback) {
            GeneralUser.count(callback);
        },
        truck_count: function(callback) {
            Truck.count(callback);
        },
        trip_count: function(callback) {
            Trip.count(callback);
        },
        location_count: function(callback) {
            TruckLocation.count(callback);
        },

    }, function(err, results) {
        res.render('index', { title: 'My App Home Page', error: err, data: results });
    });
};

What is that which I may be missing? Any hint to where I may be doing the wrong way will be highly appreciated.



via Jeemu

No comments:

Post a Comment