Javascript Mapbox地理编码器仅在获取/创建路径中部分工作。为什么我的代码不能正常工作?
我正在尝试将mapbox地理编码器添加到我的获取/创建路线。我有一个异步函数,用于地理编码器获取位置坐标。控制台日志将告诉我模型的坐标,但从mapbox geocode函数日志中提供日志中的正确坐标。从完整阵列的日志中。我的代码感觉不错,但我就是不明白为什么它不工作?这是我的密码:Javascript Mapbox地理编码器仅在获取/创建路径中部分工作。为什么我的代码不能正常工作?,javascript,node.js,mongodb,express,mapbox,Javascript,Node.js,Mongodb,Express,Mapbox,我正在尝试将mapbox地理编码器添加到我的获取/创建路线。我有一个异步函数,用于地理编码器获取位置坐标。控制台日志将告诉我模型的坐标,但从mapbox geocode函数日志中提供日志中的正确坐标。从完整阵列的日志中。我的代码感觉不错,但我就是不明白为什么它不工作?这是我的密码: async function geocoder() { try { let response = await geocodingClient .forwardGeocode({
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 ]
{坐标:[承诺{}],
_id:id,
服务:'10001',
标题:“麦米”,
地点:佛罗里达州迈阿密,
zipcode:'33101',
描述:'迈阿密',
图像:'https://i.ytimg.com/vi/9R1N_ww10wE/maxresdefault.jpg',
作者:{id:id,用户名:'username'},
联系人信息:{phoneNumber:'phone',email:'email'},
结束日期:2019-12-20T05:00:00.000Z,
创建时间:2018-10-16T15:54:23.943Z,
__v:0}
[ -80.1937, 25.7743 ]