0

Getting error when I added the location and press enter

var lat = data.results[0].geometry.location.lat;

TypeError: Cannot read property 'results' of undefined

and sometimes it gives cannot read 'geometry' of undefined error

//CREATE - add new campground to DB
router.post("/", isLoggedIn, isSafe, function(req, res){
  // get data from form and add to campgrounds array
  var name = req.body.name;
  var image = req.body.image;
  var desc = req.body.description;
  var author = {
      id: req.user._id,
      username: req.user.username
  }
  var cost = req.body.cost;
  geocoder.geocode(req.body.location, function (err, data) {
    var lat = data.results[0].geometry.location.lat;
    var lng = data.results[0].geometry.location.lng;
    var location = data.results[0].formatted_address;
    var newCampground = {name: name, image: image, description: desc, cost: cost, author:author, location: location, lat: lat, lng: lng};
    // Create a new campground and save to DB
    Campground.create(newCampground, function(err, newlyCreated){
        if(err){
            console.log(err);
        } else {
            //redirect back to campgrounds page
            console.log(newlyCreated);
            res.redirect("/campgrounds");
        }
    });
  });
});



var lat = data.results[0].geometry.location.lat;
                   ^

TypeError: Cannot read property 'results' of undefined
    at /home/shuaib/Desktop/The Web Developement Bootcamp/avasco/routes/campgrounds.js:54:20
    at Request._callback (/home/shuaib/Desktop/The Web Developement Bootcamp/avasco/node_modules/geocoder/providers/google.js:17:7)
    at Request.self.callback (/home/shuaib/Desktop/The Web Developement Bootcamp/avasco/node_modules/geocoder/node_modules/request/request.js:185:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (/home/shuaib/Desktop/The Web Developement Bootcamp/avasco/node_modules/geocoder/node_modules/request/request.js:1161:10)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (/home/shuaib/Desktop/The Web Developement Bootcamp/avasco/node_modules/geocoder/node_modules/request/request.js:1083:12)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
0xdw
  • 3,755
  • 2
  • 25
  • 40

3 Answers3

0

from google API:

geocode(request, callback)

Parameters:

request: GeocoderRequest

callback: function(Array, GeocoderStatus)

Return Value: None

Geocode a request.

Meaning- you reversed the order of "result" and "error" (status, actually) in your code.

This should be:

geocoder.geocode(req.body.location, function (**data, err**) { /*geocoder callback */ }

Community
  • 1
  • 1
Gibor
  • 1,695
  • 6
  • 20
0

Console your err object . Then you can find what should be wrong. The result is undefined because of the data is null or undefined . try this.

var geocoder = new google.maps.Geocoder();
var address = "new york";

geocoder.geocode( { 'address': address}, function(results, status) {

  if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();
    console.log(latitude,longitude);
  } 
}); 
Bivin Vinod
  • 2,210
  • 1
  • 12
  • 15
0

the data is reformatted now: the var syntax is:

      var lat = data[0].latitude;
      var lng = data[0].longitude;
      var location = data[0].formattedAddress;

The easy way to see what is returned from the api is to console.log(data)