添加位置并按 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)