javascript - NodeJS没有从函数返回数据
问题描述
我正在开发一个简单的天气应用程序,它使用只有城市名称的 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 获取结果之前跳过箭头函数的内部?
解决方案
await
仅当您等待的函数结果返回一个承诺时才等待异步操作。该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);
}
编辑 2020 年 1 月 - request() 模块处于维护模式
仅供参考,该request
模块及其衍生产品request-promise
现在处于维护模式,不会积极开发以添加新功能。您可以在此处阅读有关推理的更多信息。此表中有一个备选方案列表,并对每个备选方案进行了一些讨论。我一直在使用got()
自己,它从一开始就是使用 Promise 构建的,并且易于使用。
推荐阅读
- css - Flexbox 列,子项在 Edge 中超出范围
- elasticsearch - 如何基于metricbeat创建节拍
- sql - 将列从表添加到表
- javascript - NextJS + Express + Now 部署。无法启动,甚至 Demo 也无法启动
- javascript - 如何从nodeJS中的查询块中获取变量?
- node.js - NodeJS存储你需要解密和读取的加密数据库密码,而不是用户密码?
- apache - apache的neo4j反向代理配置
- javascript - 意外的令牌,预期的,用于 seletedValues.filter 方法
- wordpress - 在 WordPress 中重置密码后获取新的哈希密码
- go - 子例程中的 Go Channel