【问题标题】:Cannot read property 'length' of undefined ? Error in Async module无法读取未定义的属性“长度”?异步模块中的错误
【发布时间】:2018-05-15 21:07:56
【问题描述】:
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});
})

【问题讨论】:

  • 这里出了点问题。这真的是猫鼬吗?如果是这样,那么Model.find() 应该始终返回一个数组,即使没有结果也是如此。如果没有 length 属性,则返回不同的东西。如果它不是猫鼬而只是基本驱动程序,那么这些方法实际上不会返回数组,这可以解释它。那么您实际使用的是哪个驱动程序?

标签: node.js mongodb async.js


【解决方案1】:

请更正聚合甲:

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

使用喜欢:

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

【讨论】:

    猜你喜欢
    • 2015-05-08
    • 2020-06-02
    • 2017-02-15
    • 2021-03-15
    • 2015-02-05
    • 2017-11-25
    • 2021-10-21
    • 2021-02-26
    • 1970-01-01
    相关资源
    最近更新 更多