1

Using monogo's shell, db.collection.find( {key:value} ) returns exactly what I want, only I want to make this query from a browser using a URL like localhost:8000/api/profiles?key=value

I'm trying to use Express, Node and Mongoose.

Here is the code I have:

//Get By query route
router.get('/:param', function(req, res){
    var param = req.query(param);
    getProfilebyQuery(function(err, profiles) {
    if(err){
        throw err;
    }
res.json(profiles)
    })
});
//METHODS GO HERE
//These methods are stored as variables and called in the Routes above.
//Get by Query
var getProfilebyQuery = function(param, callback) {
    Iprofile.find(param, callback);
};

What you can't see is "Iprofile" requires my mongoose schemas. I can't tell what I'm doing wrong.

ple103
  • 2,080
  • 6
  • 35
  • 50
andihow
  • 13
  • 4

1 Answers1

0

Since you're using query parameters (difference between req.query and req.params ), you don't need to define /:param. You need to use req.query.

Also you are not passing the param variable to function.

//Get By query route
router.get('/', function(req, res){
  var param = req.query;
  getProfilebyQuery(param, function(err, profiles) { //pass param
    if(err){
      throw err;
    }
    res.json(profiles)
  })
});
//METHODS GO HERE
//These methods are stored as variables and called in the Routes above.
//Get by Query
var getProfilebyQuery = function(param, callback) {
  Iprofile.find(param, callback);
};
Community
  • 1
  • 1
Talha Awan
  • 4,573
  • 4
  • 25
  • 40