0

I have a webservice that queries the mongodb (with mongoose) with a given gps data, this is how it looks:

app.get('/queryUsers/:latitude,:longitude,:distance', function(req, res) {


        var lat = req.params.latitude;
        var long = req.params.longitude;
        var distance = req.params.distance;


        // Opens a generic Mongoose Query. Depending on the post body we will...
        var query = HelpRequest.find({});

        // ...include filter by Max Distance (converting miles to meters)
        if (distance) {

            // Using MongoDB's geospatial querying features. (Note how coordinates are set [long, lat]
            query = query.where('location').near({
                center: {type: 'Point', coordinates: [long, lat]},

                // Converting meters to miles. Specifying spherical geometry (for globe)
                maxDistance: distance * 1609.34, spherical: true
            });
        }

etc.

When I call it with a correct values, e.g.:

http://localhost:3000/queryUsers/48.77824506989009,19.80828625000005,1691.758035687216

then I'm getting the correct data. But when I call it for example with string:

http://localhost:3000/queryUsers/48.77824506989009,someString,1691.758035687216

then I'm getting this error:

{"stack":"Error\n    at MongooseError.CastError 
(/project/node_modules/mongoose/lib/error/cast.js:18:16)\n    at SchemaArray.SchemaNumber.cast 
(/project/node_modules/mongoose/lib/schema/number.js:219:9)\n    at SchemaArray.castToNumber 
(/project/node_modules/mongoose/lib/schema/array.js:227:38)\n    at /project/node_modules/mongoose/lib/schema/array.js:237:29\n    at Array.forEach (native)\n    at castArraysOfNumbers 
(/project/node_modules/mongoose/lib/schema/array.js:233:7)\n    at cast$geometry (/project/node_modules/mongoose/lib/schema/array.js:260:7)\n    at SchemaArray.cast$near 
(/project/node_modules/mongoose/lib/schema/array.js:249:12)\n    at SchemaArray.castForQuery
 (/project/node_modules/mongoose/lib/schema/array.js:190:19)\n    at module.exports 

Now, since I'm new to writing such webservices - could you help me and tell me how should I modify my code to return a nicer message to the end user in case of giving the wrong data?

randomuser1
  • 2,733
  • 6
  • 32
  • 68

1 Answers1

1

Maybe you can validate each of the numbers and return errors if they are not what you expect them to be, that way you can intercept the error before it happens in Mongoose which will be much faster since it doesn't hit the DB to do validation there.

Since lat and long are float numbers, you can validate if they are in fact, float numbers. The following link can help you with that.

How do I check that a number is float or integer?

To actually validate, you could use middleware or do it in the same method you're using but middleware is more "node.js".

    app.get('/queryUsers/:latitude,:longitude,:distance', validateLocation, function(req, res) {
// data is correct here
});

and then create a new method to do the validation:

function validateLocation(req, res, next) {
   var lat = req.params.latitude;
   if(!isFloat(lat)) { return res.status(406).send("Please send a valid latitude"); } 
// ... do the same for each variable. you can also use arrays to return multiple errors
   else {
     return next();
   }
}
Community
  • 1
  • 1
Luis Elizondo
  • 1,979
  • 2
  • 14
  • 15