2016-04-02 6 views
0

私は、mongoDBコレクション内のドキュメントをフィルタリングし、金曜日、土曜日、または日曜日に一致する日付を持つすべての結果を返す関数を持っています。これは、期待どおりに動作します。しかし、今週末に来るかどうかを判断するためにこれらの結果を一致させる必要がありますが、私のwhileループは3を返すべきときに1つの結果しか返しません。何がうまくいかないのですか?ループ中に何が問題になっていますか?

//FIND ALL ENTRIES THAT FALL ON A WEEKEND 
function weekendPlans(callback) { 
    Entry.aggregate(
     [ 
      { "$redact": { 
       "$cond": { 
        "if": { 
         "$or": [ 
          { "$eq": [ { "$dayOfWeek": "$selectedDate" }, 1 ] }, 
          { "$eq": [ { "$dayOfWeek": "$selectedDate" }, 6 ] }, 
          { "$eq": [ { "$dayOfWeek": "$selectedDate" }, 7 ] } 
         ] 
        }, 
        "then": "$$KEEP", 
        "else": "$$PRUNE" 
       } 
      }} 
     ], 
     // GET THE RESULTS AND RETURN IF selectedDate MATCHES THIS WEEKEND 
     function(err,results) { 
     var i = results.length; 
     var theWeekend; 
     console.log(results) 

     // EVERYTHING WORKS UNTIL HERE 
     while(i--) { 
      if(results[i].selectedDate === friday || saturday || sunday) { 
       theWeekend = results[i]; 
       //console.log(theWeekend); 
       break; 
      } 
     } 
     callback(err, theWeekend) 
     } 
)}; 

期待される結果:

[ { _id: 56fffb6ceb76276c8f39e3f4, 
    url: 'http://wellnessmama.com/13700/benefits-coconut-oil-pets/', 
    title: 'Benefits of Coconut Oil for Pets - Wellness Mama', 
    selectedDate: Sat Apr 02 2016 01:00:00 GMT+0100 (BST), 
    __v: 0 }, 
    { _id: 56fffb8eeb76276c8f39e3f5, 
    url: 'https://news.ycombinator.com/item?id=11404770', 
    title: 'The Trouble with CloudFlare | Hacker News', 
    selectedDate: Sun Apr 03 2016 01:00:00 GMT+0100 (BST), 
    __v: 0 }, 
    { _id: 56fffb5ceb76276c8f39e3f3, 
    url: 'http://londonist.com/2015/11/where-to-eat-and-drink-in-balham', 
    title: 'Where To Eat And Drink In... Balham | Londonist', 
    selectedDate: Fri Apr 01 2016 01:00:00 GMT+0100 (BST), 
    __v: 0 } ] 

現在の結果:

{ _id: 56fffb5ceb76276c8f39e3f3, 
    url: 'http://londonist.com/2015/11/where-to-eat-and-drink-in-balham', 
    title: 'Where To Eat And Drink In... Balham | Londonist', 
    selectedDate: Fri Apr 01 2016 01:00:00 GMT+0100 (BST), 
    __v: 0 } 
+0

「改行」ですか? – Thomas

答えて

0

変更

results[i].selectedDate === friday || saturday || sunday 

~results[i].selectedDate.indexOf('Fri') || 
~results[i].selectedDate.indexOf('Sat') || 
~results[i].selectedDate.indexOf('Sun') 

すべての変数を値でチェックし、日付文字列が日付文字列内にあるかどうかをチェックする必要があるためです。

関連する問題