api - React-Native Promise.all 和多个 API 请求
问题描述
我正在尝试从 OpenWeather 中提取 2 个不同的 API 请求,用于使用 React-Native 编写的天气应用程序。无论如何,我是使用 API 的新手,但我也在尝试同时使用 2。我已经浏览了十几个教程,但无法使其正常工作。最讨厌的一个看起来很直接,但我不断收到 linting 错误。任何帮助,将不胜感激。这是最新的代码/尝试。我的 linter 一直说 ; 或者,在...之后
let currentWeather => finalVals[0];
let dailyWeather => finalVals[1];
...是错误的,但改变它们也无济于事。这是我也在使用的这个教程的链接:https ://medium.com/@gianpaul.r/fetching-from-multiple-api-endpoints-at-once-ffb1b54600f9
这是我的代码:
_getWeather = (lat, long) => {
let currentWeather = fetch(`http://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&APPID=${API_KEY}`);
let dailyWeather = fetch(`http://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${long}&APPID=${API_KEY}`);
Promise.all([currentWeather, dailyWeather])
.then(values => Promise.all(values.map(value => value.json())))
.then(finalVals => {
let currentWeather => finalVals[0];
let dailyWeather => finalVals[1];
this.setState({
temperature: Math.floor((currentWeather.main.temp) - 273.15),
name: currentWeather.weather[0].main,
location: currentWeather.name,
isLoaded: true
});
});
};
解决方案
叹息......在我的头撞到墙上一个小时后,我意识到我在不应该有的地方有功能箭头。除非有人发现此方法有任何其他问题,否则对这篇文章表示最深切的歉意。
推荐阅读
- permissions - 用于访问 Google Cloud Storage 上的 HTML 文件的 Google Data Studio 服务帐户
- python - Windows Docker - 在 python 中安装 Spacy 语言模型返回 ImportError:DLL 加载失败:找不到指定的模块
- azure - 无法将 Service Fabric SDK 4.1.409 安装到 Windows 10 笔记本电脑
- javascript - 使用firebase中的where子句检索数据
- excel - Power Query:如何使 Power Query 允许具有相同名称的列?
- javascript - 单击时如何显示未来图像的实际大小
- oracle - 如何获取 oracle dataguard 日志传输和应用状态?
- dns - 引用相同 A 记录的 CNAME 的不同 nslookup 响应问题
- python - 在 Python 中并行化函数
- image-processing - pytorch affine_grid:theta 输入是什么?