簡體   English   中英

使用mongojs節點中的區別失敗

[英]Distinct in node fails using mongojs

我對node和mongodb相當陌生,但是我試圖為監視應用程序編寫一個簡單的API。 我可以通過/aps調用列出所有可用的/aps ,但是/locations會拋出:

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

TypeError: cb不是函數

我有一個具有以下架構的集合:

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

//獲得所有aps作品的魅力

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);
    });
});

但是以下方法不起作用:

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

問題是,當我在mongodb中執行db.ap_info.distinct('details.aplocation')時,它起作用了,我在這里缺少什么?

以下是mongojs中不同方法的簽名

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

因此,您需要將查詢作為函數的第二個參數包括在內:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM