0

I am using node.js and mongodb for geolocation app and req Param returns an empty array

exports.findpressure = function(req, res) {
var queryObject = req.param('q');
console.log(queryObject);
db.collection('Pressure', function(err, collection) {
collection.find( { loc: { $near :[  req.param('q') ] , $maxDistance : 5 }},{"value" : 1, _id : 0})  .sort({_id : -1}).limit(1).toArray(function(err, items) 

 {              
        res.send(items);            

    });
});
};

the longitude and latitude values are displayed in console

Listening on port 3000...

Connected to 'weather' database

8.9068256,52.019347499999995

GET /pressure?q=8.9068256,52.019347499999995 200 27ms - 2b

the url is as follows

http://localhost:3000/pressure?q=8.9068256,52.019347499999995

if i use the values like 8.9068256,52.019347499999995 its getting the value from the database but if i use req.param('q') its returning an empty array

1
  • Normally we use req.params for the following This property is an object containing properties mapped to the named route "parameters". For example if you have the route /user/:name, then the "name" property is available to you as req.params.name. This object defaults to {}. // GET /user/tj req.params.name // => "tj" Commented Apr 22, 2014 at 12:37

1 Answer 1

2

You seem to be passing those values as part of the query string. req.query.q should contain those comma-separated values

EDIT: Looking at req.param() it should be grabbing that query value. Are you expecting those values to be an strings? Should they be separate strings? At the very least that array might need to be given req.param('q').split(','). If they need to be numbers, req.param('q').split(',').map(function(val) {return +val;});.

Sign up to request clarification or add additional context in comments.

3 Comments

req.param('q').split(',').map(function(val) {return +val;}); returns the value Thanks and it would be great if you can explain it in detail as how it works
req.param('q') gives us the string '8.9068256,52.019347499999995'. split(',') splits on the comma and gives us an array of ['8.9068256', '52.019347499999995']. Mapping those strings as +val (the arg being passed to the map function) causes JS to interpret those strings as numbers, giving [8.9068256, 52.019347499999995] which your db can process.
localhost:4000/#weather?key=8.9056,52.031862301 how do i remove the # i used the following code var queryObject = req.param('key').replace('#','').split(',').map(function(val) {return +val;}); but its is not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.