Jumy Elerossë (9) [Avatar] Offline
#1
Hi.

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
#2
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 (3) [Avatar] Offline
#3
So it happens they no longer support this method in latest Mongo/Mongoose.
geoNear

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

https://github.com/Automattic/mongoose/blob/d7364dc9cbe297ba04883db044bbd70ea53434a5/migrating_to_5.md#geonear

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 (130) [Avatar] Offline
#4
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.

Thanks!