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?