Jumy Elerossë (9) [Avatar] Offline

I'm having a problem in chapter 7. I'm even using the files from GitHub but the problem persists. When I open the home page, where all the locations should be listed, I have the API lookup error instead. Displaying in console the data causing the error 500, I get this:

GET /api/locations?lng=-0.7992599&lat=51.378091&maxDistance=20 500 172.504 ms - 3312
<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1.0"><title></title><link rel="stylesheet" href="/stylesheets/bootstrap.min.css"><link rel="stylesheet" href="/stylesheets/font-awesome.min.css"><link rel="stylesheet" href="/stylesheets/tether.min.css"><link rel="stylesheet" href="/stylesheets/style.css"></head><body><nav class="navbar navbar-expand-lg navbar-dark bg-dark"><a class="navbar-brand" href="/">Loc8r</a><div class="container"><button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor02" aria-controls="navbarColor02" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse" id="navbarColor02"><ul class="navbar-nav mr-auto"><li class="nav-item active"><a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a></li><li class="nav-item"><a class="nav-link" href="/about/">About</a></li></ul></div></div></nav><div class="container"><h1>Loc.geoNear is not a function</h1><h2></h2><pre>TypeError: Loc.geoNear is not a function

Therefore, the cause is something with geoNear. I spent hours and hours trying to sort this problem, but I only managed to have a huge headache. Does anyone has any idea what can be failing? I'm sure that the cause it's something simple and evident, but I can't find it.

Thanks for your help.
Jumy Elerossë (9) [Avatar] Offline
Ok, it seems like the cause is mongoose's version. Using "mongoose": "^4.9.1" instead of "mongoose": "^5.0.1" (which is installed by default with Express at the moment of writing this) in package.json seems to solve the problem. I guess in the new version of mongoose geonear is used in a different way...
476453 (4) [Avatar] Offline
So it happens they no longer support this method in latest Mongo/Mongoose.

Model.geoNear() has been removed because the MongoDB driver no longer supports it


At first I was upset about the fact that we are being introduced to a date release of a library, but then I thought that it is not what the book is about. It is important to notice at the beginning of the book, though, that a certain version of mongoose is required. As far as I remember there is just a recommendation as for now.
331872 (131) [Avatar] Offline
Hi, I'm Luis the editor of the book, I'll try to help answer this question.

First of thanks and apologize for the issue. Yes, I think since the focus is really the MEAN stack, possibly a Mongoose call is not the primary focus, as you said. The authors will either use a fixed version of the package that guarantees a working geoNear or possibly find a replacement for this. We'll make a note on this.

565611 (1) [Avatar] Offline
you can use the $geoNear aggregation in the api locations controller, it is supported without changes and be combined with other aggregations. The function "locationsListByDistance" looks then as follows (lines 18ff.)

const locationsListByDistance = function (req, res) {
  const lng = parseFloat(req.query.lng);
  const lat = parseFloat(req.query.lat);
  const maxDistance = parseFloat(req.query.maxDistance);
  const point = {
    type: "Point",
    coordinates: [lng, lat]
  if ((!lng && lng !==0 ) || (!lat && lat !== 0) || !maxDistance) {
        message : 'lng, lat and maxDistance query parameters are all required'

      $geoNear: {
        near: point,
        maxDistance: maxDistance,
        key: "coords",
        distanceField: "distance",
        spherical: true,
        query: 'find( )'
    .exec((err, results) => {   // add error handling
      if (err) {
      } else {
          .status(200)  // 200: ok
537960 (1) [Avatar] Offline
Hi Luis.
Such a well developed book and thoroughly planned out. Many thanks to you, Simon and the team!

This is great, it totally worked. I updated the controllers/locations.js file with this code and now have a 'No Places Found Nearby' message, where as I had the 'API Lookup Error' before modifying.
331872 (131) [Avatar] Offline
Glad you liked it. Thanks!