我可以控制台记录两者results.data.features,results.data.features[0].center当我控制台时,results.data.features[0].center它显示该位置的数据,但是当我在.get请求中调用它时,它返回未定义。我不确定我做错了什么,任何指导都会有很大的帮助!
router.get('/api/search/:location', async (req, res) => {
const searchLocation = req.params.location;
const geocode_res = await geocode(searchLocation, ({ latitude, longitude }) => {
searchFunction(latitude, longitude)
})
console.log(geocode_res)
res.send(geocode_res)
})
module.exports = {
async searchFunction(latitude, longitude){
const URL = `https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&latitude=${latitude}&longitude=${longitude}&maxradiuskm=50`
try {
let results = await axios.get(URL)
return results.data.features
} catch (err) {
console.log(`${err}`)
}
},
async geocode(location, callback) {
try{
const GEO_URL = `https://api.mapbox.com/geocoding/v5/mapbox.places/${location}.json?access_token=${process.env.MAPBOX_API}`;
let results = await axios.get(GEO_URL)
const latLong = results.data.features[0].center
callback({
latitude: latLong[1],
longitude: latLong[0]
})
}catch(error){
console.log(` ${error}`)
}
},
}
慕莱坞森
相关分类