【发布时间】:2019-11-06 16:35:14
【问题描述】:
我想用上一个 API 的数据创建另一个 API,但我不知道如何使用 axios 发出多个请求。我尝试了一些东西,但它没有显示任何东西。我想显示上一个 API 的路线数据。
这是为了制作另一个最简单的API。
async function temperature() {
this.start = '2017-01-01';
try {
this.latlongdata = await axios.get('https://api.apixu.com/v1/current.json?key=' + WEATHER_KEY + '&q=' + 'navodari');
this.lat = latlongdata.data.location.lat;
this.lon = latlongdata.data.location.lon;
console.log(lat);
this.stationdata = await axios.get('https://api.meteostat.net/v1/stations/nearby?lat=' + this.lat + '&lon=' + this.lon + '&limit=1&key=' + STATION_KEY);
this.station = stationdata.data.data[0].id;
this.tempdata = await axios.get('https://api.meteostat.net/v1/history/daily?station=' + this.station + '&start=' + this.start + '&end=' + this.start + '&key=' + STATION_KEY);
return this.tempdata;
} catch (err) {
console.log(err);
}
};
class WeatherController {
index ({ response }) {
response.send(temperature());
}
}
module.exports = WeatherController
我想显示来自this.tempdata 的整个 API。
【问题讨论】:
-
“这不起作用。” - 它怎么不工作?是否报告了任何错误?
-
哦,抱歉,我会解决这个问题,它没有在屏幕上显示任何内容。整个页面是空白的。
-
WheatherController是如何使用的?请注意temperature()返回一个承诺 - 也许response.send无法处理? -
WeatherController 是我主机上的路由 'const vreme = '/vreme' Route.get(vreme, 'WeatherController.index')'
-
您需要使用调试器逐步完成它。您的浏览器控制台中是否显示任何内容?
标签: node.js api async-await request axios