Search code examples
node.jsmongoosemongoose-schemaexpress-router

I want to add a function in where() in Model.find() method but its giving typo error included in Body


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)

Solution

  • 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));