我正在开发一个简单的天气应用程序,该应用程序使用的mysql数据库仅包含城市名称。然后我在数据库中查询所有名称,然后向openweathermap的API发送请求以获取天气信息。
function getCities() {
con.connect();
con.query(('SELECT city_name FROM cities'), (err, res) => {
console.log(res);
getWeather(cities);
});
};
async function getWeather(cities) {
var data = [];
for (var i = 0; i < cities.length; i++) {
var url = `http://api.openweathermap.org/data/2.5/weather?q=${cities[i].city_name}&units=metric&appid=271d1234d3f497eed5b1d80a07b3fcd1`;
await request(url, (err, res, body) => {
var json = JSON.parse(body);
var weather = {
city: json.name,
temperature: json.main.temp,
description: json.weather[0].description,
icon: json.weather[0].icon
};
data.push(weather);
});
}
console.log(data);
}
getCities()
函数可以正常工作并返回所有城市,但是在getWeather函数中发生的错误尤其是这些错误:
Desktop/WeatherApp/node_modules/request-promise-core/lib/plumbing.js:130
throw thrownException;
^
TypeError: Cannot read property 'temp' of undefined
at Request.request [as _rp_callbackOrig] (/home/kristijan/Desktop/WeatherApp/app.js:60:41)
at Request.plumbing.callback (/home/kristijan/Desktop/WeatherApp/node_modules/request-promise-core/lib/plumbing.js:76:39)
at Request.RP$callback [as _callback] (/home/kristijan/Desktop/WeatherApp/node_modules/request-promise-core/lib/plumbing.js:46:31)
at Request.self.callback (/home/kristijan/Desktop/WeatherApp/node_modules/request/request.js:185:22)
at Request.emit (events.js:182:13)
at Request.<anonymous> (/home/kristijan/Desktop/WeatherApp/node_modules/request/request.js:1161:10)
at Request.emit (events.js:182:13)
at IncomingMessage.<anonymous> (/home/kristijan/Desktop/WeatherApp/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:273:13)
at IncomingMessage.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
据我所知,它是在从API获取结果之前跳过了箭头功能的内部?
答案 0 :(得分:1)
await
仅在等待的函数结果返回promise时才等待异步操作。 request()
函数不会返回承诺(它与您传递的回调一起使用),因此await
不会等待该结果。您可以使用request-promise
库来代替,它确实会返回一个诺言,并且不会向其传递回调。
这是一个例子:
const rp = require('request-promise');
async function getWeather(cities) {
let data = [];
for (let i = 0; i < cities.length; i++) {
let url = `http://api.openweathermap.org/data/2.5/weather?q=${cities[i].city_name}&units=metric&appid=271d1234d3f497eed5b1d80a07b3fcd1`;
let body = await rp(url);
let json = JSON.parse(body);
let weather = {
city: json.name,
temperature: json.main.temp,
description: json.weather[0].description,
icon: json.weather[0].icon
};
data.push(weather);
}
console.log(data);
}
请注意,您还可以让请求API自动为您解析JSON:
const rp = require('request-promise');
async function getWeather(cities) {
let data = [];
for (let i = 0; i < cities.length; i++) {
let url = `http://api.openweathermap.org/data/2.5/weather?q=${cities[i].city_name}&units=metric&appid=271d1234d3f497eed5b1d80a07b3fcd1`;
let json = await rp({uri:url, json: true});
let weather = {
city: json.name,
temperature: json.main.temp,
description: json.weather[0].description,
icon: json.weather[0].icon
};
data.push(weather);
}
console.log(data);
}