无法读取未定义的属性“长度”?异步模块出错

时间:2018-05-15 10:27:36

标签: node.js mongodb async.js

function(callback){
                Club.aggregate({
                    $group: {
                        _id: "$positioninclub"
                    }
                }, (err, newResult) => {
                   callback(err, newResult) ;
                });
            },

我的代码在这部分有问题...我试图在异步模块的并行方法中添加此代码。

router.get('/',ensureAuthentication,function(req,res){
async.parallel([
    function(callback){
        //find function return the array
        Club.find({},(err,result) => {
            callback(err,result);
        });
    },
    function(callback){
                Club.aggregate({
                    $group: {
                        _id: "$positioninclub"
                    }
                }, (err, newResult) => {
                   callback(err, newResult) ;
                });
            },
],(err,results) => {
    const resl = results[0];
    const resl1 = results[1];

    console.log(resl);
    const dataChunk = [];
    const chunkSize = 3;

    for(let i = 0;i < resl.length ; i = i + chunkSize){
        dataChunk.push(resl.slice(i,i+chunkSize));
    }
    console.log(dataChunk);
    res.render('dashboard', {title: 'Portal', resl: resl});
})

1 个答案:

答案 0 :(得分:0)

请更正聚合甲酸盐:

 Model.aggregate([{}]) instead of Model.aggregate({})

使用赞:

Club.aggregate([{
                    $group: {
                        _id: "$positioninclub"
                    }
                }], (err, newResult) => {
                   callback(err, newResult) ;
                });