gpt4 book ai didi

javascript - 无法读取未定义的属性 'results' - var lat = data.results[0].geometry.location.lat;

转载 作者:行者123 更新时间:2023-11-30 13:54:37 26 4
gpt4 key购买 nike

添加位置并按回车键时出现错误

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

类型错误:无法读取未定义的属性“结果”

有时它会给出 cannot read 'geometry' 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)

最佳答案

来自谷歌 API:

geocode(request, callback)

Parameters:

request: GeocoderRequest

callback: function(Array, GeocoderStatus)

Return Value: None

Geocode a request.

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

这应该是:

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

关于javascript - 无法读取未定义的属性 'results' - var lat = data.results[0].geometry.location.lat;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57545507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com