我正在尝试将地图框地理编码器添加到我的获取/创建路线。我为Geocoder提供了一个异步函数来获取位置的坐标。控制台日志会告诉我对模型坐标的未决承诺,但会在mapbox地理编码功能日志的日志中给我正确的corcordins。从完整阵列的日志。我的代码感觉不错,但是我不明白为什么它不起作用?这是我的代码:
async function geocoder() {
try {
let response = await geocodingClient
.forwardGeocode({
query: req.body.location,
limit: 1
})
.send()
var coordinates = response.body.features[0].geometry.coordinates;
req.body.coordinates = response.body.features[0].geometry.coordinates;
console.log(response.body.features[0].geometry.coordinates);
return coordinates
} catch(err){
console.log(err.message);
}
}
// get data from forum and add to post array
var home = req.body.home;
var title = req.body.title;
var location = req.body.location;
var zipcode = req.body.zipcode;
var coordinates = geocoder(req.body.location);
var dsc = req.body.description;
var image = req.body.image;
var endDate = req.body.endDate
var author = {
id: req.user._id,
username: req.user.username
};
var contactInfo = {
phoneNumber: formatPhoneNumber(req.body.phoneNumber),
email: req.body.email
}
var newHome = {
data: data,
title: title,
location: location,
zipcode: zipcode,
coordinates: coordinates,
description: dsc,
image: image,
author: author,
contactInfo: contactInfo,
endDate: endDate,
};
控制台输出为:
{ coordinates: [ Promise { <pending> } ],
_id: id,
service: '10001',
title: 'Maimi',
location: 'Miami, Fl',
zipcode: '33101',
description: 'Miami',
image: 'https://i.ytimg.com/vi/9R1N_ww10wE/maxresdefault.jpg',
author: { id: id, username: 'username' },
contactInfo: { phoneNumber: ‘phone', email: 'email' },
endDate: 2019-12-20T05:00:00.000Z,
created: 2018-10-16T15:54:23.943Z,
__v: 0 }
[ -80.1937, 25.7743 ]