Tuesday, 4 April 2017

I get the 304 error in the git terminal **"GET /api/locations?lng=-0.79&lat=51.3&maxDistance=20000 304 32.331 ms - -

Problem Hi i am currently working through the example in Getting Mean with Mongo Express Angular and Node js.

I get the 304 error in the git terminal "GET /api/locations?lng=-0.79&lat=51.3&maxDistance=200000000 304 32.331 ms - -" while trying to query the database. I used the Angular $HTTP service to make the request to the API as suggested in the book but the geonear function returns an empty array []. I have being on it for some time now.

Pls can anyone tell me how to fix this error? Tnx

What I have done I located the error to the controller. It gets the query and passeses the query to geoNear function. The geoNear function then returns the locations array as empty which is very wrong.

// Controller handling the request
    module.exports.locationsListByDistance = function(req, res) {
      var lng = parseFloat(req.query.lng);
      var lat = parseFloat(req.query.lat);
      var point = {
        type: "Point",
        coordinates: [lng, lat]
      };
      var geoOptions = {//Works in metres
        spherical: true,
        maxDistance: 20000,
        num: 10
      };

      if ((!lng && lng!==0)||(!lat && lat!==0))  {
        sendJSONresponse(res, 404, {
          "message": "lng, lat and maxDistance query parameters are all required"
        });
        return;
      }
      console.log(geoOptions);
      console.log(point);
      Loc.geoNear(point, geoOptions, function(err, results, stats) {
        console.log(results);
        var locations = [];
        if (err) {
          sendJSONresponse(res, 404, err);
          console.log(err);
        } else {
            results.forEach(function(doc) {
               locations.push({
                  distance: doc.dis,
                  name: doc.obj.name,
                  address: doc.obj.address,
                  rating: doc.obj.rating,
                  facilities: doc.obj.facilities,
                  _id: doc.obj._id
                });
              });
             sendJSONresponse(res, 200, locations);
             console.log(locations);
        }
      });
    };

Error in terminal



via user3717041

No comments:

Post a Comment