2014-07-08 2 views
2

시간 버킷을 사용하여 집계하려고합니다. 그런 다음 하루에 값이 가득 찬 배열을 반환하려고합니다 (문서가 없을 때 0). aggeagate 기능 작업 wondefully,이 같은 콜백 (이전 CONSOLE.LOG) 교체 할 때이지만 :RangeError : 최대 호출 스택 크기가 몽구스로 초과되었습니다.

Star.aggregate([{ 
    $match: { 
     "mod": new mongoose.Types.ObjectId("53765a122c0cda28199df3f4"), 
     "time_bucket.month": new TimeBucket().month 
    } 
    }, 
    { 
    $group: { 
     _id: "$time_bucket.day", 
     stars: { 
     "$sum": 1 
     } 
    } 

     }, 
    { 
    $sort: { 
     '_id': 1 
    } 
     }], 
function (err, mods) { 
    console.log(err, mods); 
    mods = mods.map(function (model) { 
    return model.toObject ? model.toObject() : model 
    }); 
    var i, time, docs = []; 
    var hasGap = function (a, b) { 
    a = a.replace(/-/g, ''); 
    b = b.replace(/-/g, ''); 
    return a - b !== 1 
    } 

    var process = function (i, mods, docs) { 
    if (hasGap(mods[i]._id, mods[i + 1]._id)) { 
     docs.push(0); 
     return process(i, mods, docs) 
    } 
    docs.push(mods[i].stars); 
    return process(i + 1, mods, docs) 

    }; 
    process(0, mods, docs); 
    console.log(docs); 
}); 

를 내가 얻을 :

C:\Users\Me\Nodejs\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\base.js:245 
     throw message; 
      ^
RangeError: Maximum call stack size exceeded 

답변

2

귀하의 process 기능은 재귀하고 나누기 어떤 경로가 없습니다 재 호출을 process으로 다시 호출하지 마십시오. 고전적인 무한 재귀 스택 오버 플로우.

해결책은 mods 결과 문서 배열의 끝을 확인하고 다시 자체를 호출하는 대신 그 시점으로 돌아가도록 process 함수를 변경하는 것입니다.

그래서 같은 검사 :

if (i >= mods.length) return; 
+0

그리고 해결책? –

+0

@Julien 업데이트 된 답변보기. OP의 데이터에 대해 더 알지 못하면 더 구체적으로 표현하기가 어렵습니다. – JohnnyHK

관련 문제