Unable to use geoNear in my nodeJS application

35 Views Asked by At

I'm unable to use the geoNear function in my nodeJS application.

Ninja.aggregate([
        {
            $geoNear: {
                near: {
                    type: "Point",
                    coordinates: [parseFloat(req.query.lng), parseFloat(req.query.lat)]
                },
                distanceField: "dist.calculated",
                spherical: true,
                maxDistance: 100000

            }
        }
    ])
        .then(function (ninja) {
            res.send(ninja);
        }).catch(next);

Error obatined: { "error": "geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 100000.0" }

1

There are 1 best solutions below

3
facebook-10203229637341337 On

Please check your input values for req.query.lng, req.query.lat Keep coordinates as [longitude, latitude]

$geoNear: {
  near: {
    type: "Point",
    coordinates: [ <LONGITUDE>, <LATITUDE>]
  },
  distanceField: "dist.calculated",
  spherical: true,
  maxDistance: 100000
}

where, LONGITUDE ranges from -180 to 180, LATITUDE ranges from -90 to 90 Please make sure you have received valid latitude and longitude