2014-09-29 519 views
1

我送查询参数为JSON格式req.query.p从我的前端MVC框架,问题是,这可能是一个动态密钥和值,例如:sequelize动态查询参数

req.query.p = {nombre : 'juan'} 

req.query.p = {pais : 'chile'} 

所以我需要钥匙,和值将它们放在where语句,像这样

exports.select = function(req, res){ 
    console.log('=> GET | Obtener peliculas'.bold.get); 
     db.Pelicula 
      .findAndCountAll({ 
       limit : req.query.limit, 
       offset : req.query.offset, 
       where : req.query.p ? [req.query.p.KEY + " = ?", req.query.p.VAL] : null 
      }) 
      .success(function(resp){ 
       console.log(JSON.stringify(resp.rows, null, 4).bold.get); 
       res.json({peliculas : resp.rows, meta : { total : resp.count}}); 
      }); 
} 

回答

2

Where参数可以是一个对象,所以你可以通过where: req.query.p

+0

谢谢!它的作品:P。顺便说一句,如果我想传递多个参数给我的查询呢?哪个是正确的方法来处理这个问题? – jFranciscov 2014-09-29 18:32:48

+0

'其中:{arg1:something,arg2:somethingelse}' - 普通的旧javascript对象 – 2014-09-30 07:23:12

+0

那里有什么操作? “...其中arg1 =某事AND/OR?arg2 = somethingelse” – jFranciscov 2014-09-30 11:52:49