2016-06-28 50 views
1

我有一個應用程序路線,我希望能夠使用查詢參數爲我的where子句,如果有查詢存在。我最初的方法是在get中使用if/else子句,並返回兩個不同的查詢,具體取決於查詢參數是否存在,但我在then(function...處得到SyntaxError: Unexpected token .錯誤,這告訴我這不是正確的方法。我如何用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對象。 ('/')

.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); 
    }) 
});