Programmatically build dynamic query with Mongoose

前端 未结 3 1187
余生分开走
余生分开走 2020-12-24 14:44

I\'m trying to build a search from input received from a form.

router.get(\'/data\', function(req, res) {
    var firstName=req.body.firstName,
    lastName         


        
3条回答
  •  一个人的身影
    2020-12-24 15:31

    To avoid checking each parameter independently you can loop through them.

    var query = {};
    for(var key in req.body){ //could also be req.query and req.params
      req.body[key] !== "" ? query[key] = req.body[key] : null;
    }
    
    mongoose.model('customers').find(query, function(err, customers){
    
    })
    

提交回复
热议问题