Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Francesco Di Vito
Francesco Di Vito
4,502 Points

Node JS mongoDB Geo Query Rest Service

Hi at all, after some great help of comunity i have develop a Rest Service with Node JS and Mongodb. I have develop a get Rest Service as this:

router.route("/api/geo_cars")
    .get(function(req,res){
        var max = 300;
        var response = {};
        mongoOOp.find({
     location:
       { $near :
          {
            $geometry: { type: "Point",  coordinates: [ 12.560945, 41.957482 ] },
            $maxDistance: max
          }
       }
   },function(err,data){
        // Mongo command to fetch all data from collection.
            if(err) {
                response = {"error" : true,"message" : "Error fetching data"};
            } else {
                response = {"error" : false,"message" : data};
            }
            res.json(response);
        });
    });

this services give me all elemtn near 300 m respect a specific coordinate.

Now i would like to pass maxDistance value of query as parameter to services and i have change the code in this way:

router.route("/api/geo_cars/:maxDistance")
    .get(function(req,res){
        var response = {};
        mongoOOp.find({
     location:
       { $near :
          {
            $geometry: { type: "Point",  coordinates: [ 12.560945, 41.957482 ] },
            $maxDistance: req.params.maxDistance
          }
       }
   },function(err,data){
        // Mongo command to fetch all data from collection.
            if(err) {
                response = {"error" : true,"message" : "Error fetching data"};
            } else {
                response = {"error" : false,"message" : data};
            }
            res.json(response);
        });
    });

but the service give me error. My code is wrong? Any help is great Thanks

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Francesco,

I'm afraid I don't know the answer to your question but I've changed your post to show your code more clearly so hopefully someone else can help :)

Have a look at the Markdown Cheatsheet for posting code and other cool things to the forum