2016-06-28 7 views
1

クエリが存在する場合、where句にクエリパラメータを使用できるようにするアプリケーションルートがあります。私の最初のアプローチは、get/if節をgetで使用し、クエリパラメータが存在するかどうかによって2つの異なるクエリを返すことでしたが、というエラーが発生しました。これは正しいアプローチではないことを私に教えています。 。 Sequelizeで何かを達成するにはどうすればいいですか?SequelizeJSクエリパラメータで条件を使用する場合

/*==== /====*/ 

appRoutes.route('/') 

    .get(function(req, res){ 

     console.log(req.query.dataDateStart); 
     console.log(req.query.dataDateEnd); 

     if(req.query.dataDateStart && req.query.dataDateEnd){ 
     return models.Comment.findAll({ 
      where: { 
       dataDateStart: { 
        $gte: dateFormatting(req.body.dataDateStart) 
       }, 
       dataDateEnd: { 
        $lte: dateFormatting(req.body.dataDateEnd) 
       } 
      }, 
      order: 'commentDate DESC', 
      include: [{ 
       model: models.User, 
       where: { organizationId: req.user.organizationId }, 
       attributes: ['organizationId', 'userId'] 
      }], 
      limit: 10 
     }) 
     } else { 
     return models.Comment.findAll({ 
      order: 'commentDate DESC', 
      include: [{ 
       model: models.User, 
       where: { organizationId: req.user.organizationId }, 
       attributes: ['organizationId', 'userId'] 
      }], 
      limit: 10 
     }) 
     } 

     .then(function(comment){ 
      function feedLength(count){ 
       if (count >= 10){ 
        return 2; 
       } else { 
        return null; 
       } 
      }; 

      res.render('pages/app/activity-feed.hbs',{ 
       comment: comment, 
       user: req.user, 
       secondPage: feedLength(comment.length) 
      }); 
     }); 
    }) 

    .post(function(req, res){ 
     function dateFormatting(date){ 
      var newDate = new Date(date); 
      return moment.utc(newDate).format(); 
     } 

     console.log("This is a date test" + dateFormatting(req.body.dataDateStart)); 
     //Testing if the query will come through correctly. 
     models.Comment.findAll({ 
      order: 'commentDate DESC', 
      where: { 
       dataDateStart: { 
        $gte: dateFormatting(req.body.dataDateStart) 
       }, 
       dataDateEnd: { 
        $lte: dateFormatting(req.body.dataDateEnd) 
       } 
      }, 
      include: [{ 
       model: models.User, 
       where: { 
        organizationId: req.user.organizationId, 
       }, 
       attributes: ['organizationId', 'userId'] 
      }], 
      limit: 10 
     }).then(function(filterValues) { 
      var dataDateStart = encodeURIComponent(dateFormatting(req.body.dataDateStart)); 
      var dataDateEnd = encodeURIComponent(dateFormatting(req.body.dataDateEnd)); 
      res.redirect('/app?' + dataDateStart + '&' + dataDateEnd); 
     }).catch(function(error){ 
      res.send(error); 
     }) 
    }); 

答えて

3

これは構文エラーです。 then関数は、対象オブジェクトに対してのみ呼び出すことができます。上記のコードでは、.thenは何も適用されません。代わりに、if-elseステートメントの後に呼び出されます。

if(...) { 
    ... 
} 
else { 
    ... 
} 
// .then() is not called on any object --> syntax error 'unexpected "."' 
.then() 

whereパラメータを設定したい場合は、urlクエリに応じてwhereオブジェクトを定義できます。 appRoutes.route( '/')

.get(function(req, res){ 

    console.log(req.query.dataDateStart); 
    console.log(req.query.dataDateEnd); 

    var whereObject = {}; 
    // CHeck for queries in url 
    if(req.query.dataDateStart && req.query.dataDateEnd){ 
     whereObject = { 
      dataDateStart: { 
       $gte: dateFormatting(req.body.dataDateStart) 
      }, 
      dataDateEnd: { 
       $lte: dateFormatting(req.body.dataDateEnd) 
      } 
     }; 
    } 

    models.Comment.findAll({ 
     where: whereObject, 
     order: 'commentDate DESC', 
     include: [{ 
      model: models.User, 
      where: { organizationId: req.user.organizationId }, 
      attributes: ['organizationId', 'userId'] 
     }], 
     limit: 10 
    }) 
    .then(function(comment){ 
     function feedLength(count){ 
      if (count >= 10){ 
       return 2; 
      } else { 
       return null; 
      } 
     }; 

     res.render('pages/app/activity-feed.hbs',{ 
      comment: comment, 
      user: req.user, 
      secondPage: feedLength(comment.length) 
     }); 
    }); 
}) 

.post(function(req, res){ 
    function dateFormatting(date){ 
     var newDate = new Date(date); 
     return moment.utc(newDate).format(); 
    } 

    console.log("This is a date test" + dateFormatting(req.body.dataDateStart)); 
    //Testing if the query will come through correctly. 
    models.Comment.findAll({ 
     order: 'commentDate DESC', 
     where: { 
      dataDateStart: { 
       $gte: dateFormatting(req.body.dataDateStart) 
      }, 
      dataDateEnd: { 
       $lte: dateFormatting(req.body.dataDateEnd) 
      } 
     }, 
     include: [{ 
      model: models.User, 
      where: { 
       organizationId: req.user.organizationId, 
      }, 
      attributes: ['organizationId', 'userId'] 
     }], 
     limit: 10 
    }).then(function(filterValues) { 
     var dataDateStart = encodeURIComponent(dateFormatting(req.body.dataDateStart)); 
     var dataDateEnd = encodeURIComponent(dateFormatting(req.body.dataDateEnd)); 
     res.redirect('/app?' + dataDateStart + '&' + dataDateEnd); 
    }).catch(function(error){ 
     res.send(error); 
    }) 
}); 
関連する問題