【发布时间】:2020-04-23 02:49:33
【问题描述】:
在我看到的每个地方,每个人都在 express get 调用中使用 axios 调用。我不想那样做。我在另一个文件中有 API 调用的代码,并从我的路由器调用它。我只是不知道如何访问返回的数据。有人可以帮忙吗?
axios 调用:
function weather(geo) {
let geoLocation = `https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURIComponent(geo)}.json?access_token=pk.eyJ1IjoiY2FydGVydzIxMjQiLCJhIjoiY2s5NGNlcmY2MDAzaTNncDJwN2ozdWk2byJ9._fm9rvVvhbCJE2eN0C-gqQ&limit=1`
axios.get(geoLocation)
.then((response) => {
if (response.data.features.length === 0) {
return console.log('Unable to find location')
} else {
const long = response.data.features[0].center[1]
const lat = response.data.features[0].center[0]
const url = `http://api.weatherstack.com/current?access_key=ebe5b5662c85c2740c00d538723b44b3&query=${long},${lat}&units=f`
return axios.get(url)
}
})
.then((response) => {
if (response.data.error) {
console.log(response.data.error.info)
} else {
console.log(response.data)
let forecastData = response.data;
return forecastData
}
})
.catch((error) => {
console.log('Unable to connect to weather service')
})
}
特快电话:
router.get('/help', function (req, res) {
weather.weather(req.query.address)
res.send({
address: req.query.address,
forecast: '?'
})
})
【问题讨论】: