2

Im quite new to node and mongodb, but im trying to write a simple API for a monitoring app. I can list out all aps fine through the /aps call, but /locations throws a:

process.nextTick(function() { throw err; });
                              ^

TypeError: cb is not a function

I have a collection with the following Schema:

ap = {
"name": apname,
"details": [{
    "apmodel": apmodel,
    "apmac": apmac,
    "apipadd": apipadd,
    "apclientcount": apclientcount,
    "aplocation": aplocation
    }]
}

// Get all aps works a charm

router.get('/aps', function(req, res, next){
    db.ap_info.find().sort({name: 1}, function(err, aps){
        if(err){
            res.send(err);
        }
        res.json(aps);
    });
});

But the following does not work:

//Get unique locations with wifi
router.get('/locations', function(req, res, next){
    db.ap_info.distinct('details.aplocation', function(err, results){
        console.log(results);
    });
});

Thing is, when i execute db.ap_info.distinct('details.aplocation') in mongodb, it works, what am i missing here?

Milo
  • 3,365
  • 9
  • 30
  • 44
shakalakka
  • 105
  • 1
  • 1
  • 8

1 Answers1

1

The signature for distinct method in mongojs follows

db.collection.distinct(field, query, callback) 

so you need to include the query as the second argument in the function:

//Get unique locations with wifi
router.get('/locations', function(req, res, next){
    db.ap_info.distinct('details.aplocation', {}, function(err, results){
        console.log(results);
    });
});
chridam
  • 100,957
  • 23
  • 236
  • 235