2

Providing the code where on passing a path is giving me a type error which is included below

function maxDis(lat1, lon1, location) {
    var fields = location.split(':');

  var lat2 = parseFloat(fields[0]);
    var lon2 = parseFloat(fields[1]);
    var maxDis = parseInt(fields[2]);

    var theta = lon1 - lon2;
    var dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
        + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))
        * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60; // 60 nautical miles per degree of seperation
    dist = dist * 1852; // 1852 meters per nautical mile

    if (Math.abs(dist) < maxDis) {
        return true;
    } else {
        return false;
    }
}

Getting an error here too :

resRouter.route('/locationfilters/veg/:location')
    .options(cors.corsWithOptions, (req, res) => { res.sendStatus(200) })
    .get(cors.cors, (req, res, next) => {
        Res.find({})
            .where(maxDis(parseFloat(this.latitude), parseFloat(this.longitude), req.params.location) == true)
            .then((rest) => {
                res.statusCode = 200;
                res.setHeader('Content-Type', 'application/json');
                res.json(rest);

            }, (err) => next(err))

            .catch((err) => next(err));
    });

I'm not able to locate the path which is not of type string

Error log on performing requests ->

TypeError: path must be a string or object
    at model.Query.Query.where (/home/yash/Documents/FoodApp/conFusionServer/node_modules/mquery/lib/mquery.js:299:9)
    at resRouter.route.options.get (/home/yash/Documents/FoodApp/conFusionServer/routes/resRouter.js:351:14)
    at Layer.handle [as handle_request] (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:137:13)
    at cors (/home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:188:7)
    at /home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:224:17
    at originCallback (/home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:214:15)
    at /home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:219:13
    at optionsCallback (/home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:199:9)
    at corsMiddleware (/home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:204:7)
    at Layer.handle [as handle_request] (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:137:13)
    at next (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:131:14)
    at next (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:131:14)
    at Route.dispatch (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/layer.js:95:5)
Ôrel
  • 4,349
  • 2
  • 19
  • 34
user9717489
  • 251
  • 1
  • 4
  • 8

1 Answers1

0

You need the Model.$where() method instead of Query.where(). Since your query is using a JavaScript expression, you can either do so via Model.find({ $where: <javascript_function> }), or use the mongoose shortcut method $where via a Query chain or from your mongoose Model where the argument is a javascript string or anonymous function i.e.

Res.$where(() => maxDis(parseFloat(this.latitude), parseFloat(this.longitude), req.params.location))
    .then((rest) => {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'application/json');
        res.json(rest);

    }, (err) => next(err))

    .catch((err) => next(err));
chridam
  • 88,008
  • 19
  • 188
  • 202
  • Error: Must have a string or function for $where, Now getting this error – user9717489 Aug 24 '18 at 12:58
  • The argument for `Model.prototype.$where()` is either a javascript string or anonymous function, I have updated to include the anonymous function that calls your named function `maxDis` – chridam Aug 24 '18 at 13:09
  • thanks a lot for help. this.latitude and this.longitude is returing undefined. Is there any soultion for getting values of longitide and latitude of Res inside $(where) – user9717489 Aug 24 '18 at 16:54