0

添加位置并按 Enter 时出现错误

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

TypeError: Cannot read property 'results' of undefined

有时它给出了无法读取未定义错误的“几何”

//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)
4

3 回答 3

0

来自谷歌 API:

地理编码(请求,回调)

参数:

请求:地理编码器请求

回调:函数(数组,GeocoderStatus)

返回值:无

对请求进行地理编码。

含义-您颠倒了代码中“结果”和“错误”(实际上是状态)的顺序。

这应该是:

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

于 2019-08-18T14:43:12.020 回答
0

控制台你的err对象。然后你可以找到应该是错的。结果是undefined因为datais nullor undefined。尝试这个。

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);
  } 
}); 
于 2019-08-18T14:58:59.803 回答
0

数据现在被重新格式化:var 语法是:

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

查看从 api 返回的内容的简单方法是 console.log(data)

于 2020-02-28T04:27:08.490 回答